summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-02-02 08:54:52 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-02-02 08:54:52 +0000
commit970e90dd1572994d3d9229725e12d47b3674fcbf (patch)
tree12541348cc1fc2b1ec2e3e87aa2b526c5d34a9c7
parentbafb881c1f22781aac347cdabb845798a7753180 (diff)
* 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
-rw-r--r--ChangeLog12
-rw-r--r--enum.c41
-rw-r--r--lib/set.rb30
-rw-r--r--test/ruby/test_enum.rb12
4 files changed, 80 insertions, 15 deletions
diff --git a/ChangeLog b/ChangeLog
index a2f67e59f1..2b9f053fb1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
diff --git a/enum.c b/enum.c
index 7212279223..3c59449362 100644
--- a/enum.c
+++ b/enum.c
@@ -1618,6 +1618,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)
{
volatile VALUE result = memo->u1.value;
@@ -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);
diff --git a/lib/set.rb b/lib/set.rb
index e28ab0f5ef..3e22a8c839 100644
--- a/lib/set.rb
+++ b/lib/set.rb
@@ -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
diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb
index d252cef426..945661f4b9 100644
--- a/test/ruby/test_enum.rb
+++ b/test/ruby/test_enum.rb
@@ -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 = []