aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-06 17:14:02 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-06 17:14:02 +0000
commitce0bf9f43d9d1828bfd1d8001dcba729d9553f38 (patch)
tree6c76586cce264a0af38a782d653480e4ccb01089
parent17c0aff0d6d8a2629b535a52a93e348904e27b97 (diff)
downloadruby-ce0bf9f43d9d1828bfd1d8001dcba729d9553f38.tar.gz
* struct.c: Support for Struct's enumerators #size
[Feature #6636] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37513 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--struct.c9
-rw-r--r--test/ruby/test_enumerator.rb7
2 files changed, 13 insertions, 3 deletions
diff --git a/struct.c b/struct.c
index f0307916b3..17e53ab4e0 100644
--- a/struct.c
+++ b/struct.c
@@ -429,6 +429,9 @@ rb_struct_new(VALUE klass, ...)
return rb_class_new_instance(size, mem, klass);
}
+static VALUE
+rb_struct_size(VALUE s);
+
/*
* call-seq:
* struct.each {|obj| block } -> struct
@@ -455,7 +458,7 @@ rb_struct_each(VALUE s)
{
long i;
- RETURN_ENUMERATOR(s, 0, 0);
+ RETURN_SIZED_ENUMERATOR(s, 0, 0, rb_struct_size);
for (i=0; i<RSTRUCT_LEN(s); i++) {
rb_yield(RSTRUCT_PTR(s)[i]);
}
@@ -489,7 +492,7 @@ rb_struct_each_pair(VALUE s)
VALUE members;
long i;
- RETURN_ENUMERATOR(s, 0, 0);
+ RETURN_SIZED_ENUMERATOR(s, 0, 0, rb_struct_size);
members = rb_struct_members(s);
for (i=0; i<RSTRUCT_LEN(s); i++) {
rb_yield_values(2, rb_ary_entry(members, i), RSTRUCT_PTR(s)[i]);
@@ -793,7 +796,7 @@ rb_struct_select(int argc, VALUE *argv, VALUE s)
long i;
rb_check_arity(argc, 0, 0);
- RETURN_ENUMERATOR(s, 0, 0);
+ RETURN_SIZED_ENUMERATOR(s, 0, 0, rb_struct_size);
result = rb_ary_new();
for (i = 0; i < RSTRUCT_LEN(s); i++) {
if (RTEST(rb_yield(RSTRUCT_PTR(s)[i]))) {
diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb
index 884f8c6b27..c45e3eb0a7 100644
--- a/test/ruby/test_enumerator.rb
+++ b/test/ruby/test_enumerator.rb
@@ -456,6 +456,13 @@ class TestEnumerator < Test::Unit::TestCase
end
end
+ def test_size_for_enum_created_from_struct
+ s = Struct.new(:foo, :bar, :baz).new(1, 2)
+ %i[each each_pair select].each do |method|
+ assert_equal 3, s.send(method).size
+ end
+ end
+
def check_consistency_for_combinatorics(method)
[ [], [:a, :b, :c, :d, :e] ].product([-2, 0, 2, 5, 6]) do |array, arg|
assert_equal array.send(method, arg).to_a.size, array.send(method, arg).size,