Skip to content

Commit

Permalink
* enum.c (enum_each_entry): new method #each_entry to pack values
Browse files Browse the repository at this point in the history
  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
  • Loading branch information
matz committed Feb 2, 2010
1 parent bafb881 commit 970e90d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
12 changes: 12 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
Tue Feb 2 14:30:27 2010 Yukihiro Matsumoto <[email protected]>

* 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 <[email protected]>

* lib/delegate.rb (Delegator#marshal_dump): exclude
Expand Down
41 changes: 41 additions & 0 deletions enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
30 changes: 15 additions & 15 deletions lib/set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def initialize(enum = nil, &block) # :yields: o
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
Expand Down Expand Up @@ -123,9 +123,9 @@ def replace(enum)
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
Expand Down Expand Up @@ -281,8 +281,8 @@ def merge(enum)
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
Expand All @@ -291,8 +291,8 @@ def merge(enum)
# 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

Expand All @@ -314,9 +314,9 @@ def -(enum)
# 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 & ##
Expand Down Expand Up @@ -514,7 +514,7 @@ def initialize(*args, &block)
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
Expand Down Expand Up @@ -642,16 +642,16 @@ def to_set(klass = Set, *args, &block)
# 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
Expand Down
12 changes: 12 additions & 0 deletions test/ruby/test_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ def test_each_with_object
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 = []
Expand Down

0 comments on commit 970e90d

Please sign in to comment.