diff options
-rw-r--r-- | struct.c | 9 | ||||
-rw-r--r-- | test/ruby/test_enumerator.rb | 7 |
2 files changed, 13 insertions, 3 deletions
@@ -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, |