зеркало из https://github.com/github/ruby.git
* 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
This commit is contained in:
Родитель
17c0aff0d6
Коммит
ce0bf9f43d
9
struct.c
9
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]))) {
|
||||
|
|
|
@ -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,
|
||||
|
|
Загрузка…
Ссылка в новой задаче