1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* enum.c (enum_each_entry): new method #each_entry to pack values

from yield into an array.

* lib/set.rb (Set#merge): use Enumerable#each_entry to implement
  Set compatible to 1.8 behavior.  [ruby-core:27985]

* lib/set.rb: replace is_a?(Enumerable) with respond_to?(:each)
  for duck typing.

* lib/set.rb (SortedSet#add): typo fixed.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26540 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2010-02-02 08:54:52 +00:00
parent bafb881c1f
commit 970e90dd15
4 changed files with 80 additions and 15 deletions

View file

@ -1,3 +1,15 @@
Tue Feb 2 14:30:27 2010 Yukihiro Matsumoto <matz@ruby-lang.org>
* enum.c (enum_each_entry): new method #each_entry to pack values
from yield into an array.
* lib/set.rb (Set#merge): use Enumerable#each_entry to implement
Set compatible to 1.8 behavior. [ruby-core:27985]
* lib/set.rb: replace is_a?(Enumerable) with respond_to?(:each)
for duck typing.
* lib/set.rb (SortedSet#add): typo fixed.
Tue Feb 2 11:13:56 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/delegate.rb (Delegator#marshal_dump): exclude

41
enum.c
View file

@ -1617,6 +1617,46 @@ enum_reverse_each(int argc, VALUE *argv, VALUE obj)
}
static VALUE
each_val_i(VALUE i, VALUE p, int argc, VALUE *argv)
{
VALUE *memo = (VALUE *)p;
ENUM_WANT_SVALUE();
rb_yield(i);
return Qnil;
}
/*
* call-seq:
* enum.each_entry {|obj| block} => enum
*
* Calls <i>block</i> once for each element in <i>self</i>, passing that
* element as a parameter, converting multiple values from yield to an
* array.
*
* class Foo
* include Enumerable
* def each
* yield 1
* yield 1,2
* end
* end
* Foo.new.each_entry{|o| print o, " -- "}
*
* produces:
*
* 1 -- [1, 2] --
*/
static VALUE
enum_each_entry(int argc, VALUE *argv, VALUE obj)
{
RETURN_ENUMERATOR(obj, argc, argv);
rb_block_call(obj, id_each, argc, argv, each_val_i, 0);
return obj;
}
static VALUE
zip_ary(VALUE val, NODE *memo, int argc, VALUE *argv)
{
@ -2435,6 +2475,7 @@ Init_Enumerable(void)
rb_define_method(rb_mEnumerable, "include?", enum_member, 1);
rb_define_method(rb_mEnumerable, "each_with_index", enum_each_with_index, -1);
rb_define_method(rb_mEnumerable, "reverse_each", enum_reverse_each, -1);
rb_define_method(rb_mEnumerable, "each_entry", enum_each_entry, -1);
rb_define_method(rb_mEnumerable, "zip", enum_zip, -1);
rb_define_method(rb_mEnumerable, "take", enum_take, 1);
rb_define_method(rb_mEnumerable, "take_while", enum_take_while, 0);

View file

@ -70,8 +70,8 @@ class Set
enum.nil? and return
if block
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
enum.each { |o| add(block[o]) }
enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
enum.each_entry { |o| add(block[o]) }
else
merge(enum)
end
@ -123,9 +123,9 @@ class Set
if enum.class == self.class
@hash.replace(enum.instance_eval { @hash })
else
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
clear
enum.each { |o| add(o) }
enum.each_entry { |o| add(o) }
end
self
@ -281,8 +281,8 @@ class Set
if enum.instance_of?(self.class)
@hash.update(enum.instance_variable_get(:@hash))
else
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
enum.each { |o| add(o) }
enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
enum.each_entry { |o| add(o) }
end
self
@ -291,8 +291,8 @@ class Set
# Deletes every element that appears in the given enumerable object
# and returns self.
def subtract(enum)
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
enum.each { |o| delete(o) }
enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
enum.each_entry { |o| delete(o) }
self
end
@ -314,9 +314,9 @@ class Set
# Returns a new set containing elements common to the set and the
# given enumerable object.
def &(enum)
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
n = self.class.new
enum.each { |o| n.add(o) if include?(o) }
enum.each_entry { |o| n.add(o) if include?(o) }
n
end
alias intersection & ##
@ -514,7 +514,7 @@ class SortedSet < Set
end
def add(o)
o.respond_to?(:<=>) or raise ArgumentError, "value must repond to <=>"
o.respond_to?(:<=>) or raise ArgumentError, "value must respond to <=>"
super
end
alias << add
@ -642,16 +642,16 @@ end
# end
#
# def replace(enum)
# enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
# enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
# clear
# enum.each { |o| add(o) }
# enum.each_entry { |o| add(o) }
#
# self
# end
#
# def merge(enum)
# enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
# enum.each { |o| add(o) }
# enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
# enum.each_entry { |o| add(o) }
#
# self
# end

View file

@ -219,6 +219,18 @@ class TestEnumerable < Test::Unit::TestCase
assert_equal([55, 3628800], ret)
end
class Foo
include Enumerable
def each
yield 1
yield 1,2
end
end
def test_each_entry
assert_equal([1, [1, 2]], Foo.new.each_entry.to_a)
end
def test_zip
assert_equal([[1,1],[2,2],[3,3],[1,1],[2,2]], @obj.zip(@obj))
a = []