Project

General

Profile

Feature #19261 » data-and-struct-remove-members-method.patch

fabiormoura (Fabio Moura Maia), 01/10/2023 01:22 AM

View differences:

struct.c
return rb_ary_dup(members);
}
/*
* call-seq:
* members -> array_of_symbols
*
* Returns the member names from +self+ as an array:
*
* Customer = Struct.new(:name, :address, :zip)
* Customer.new.members # => [:name, :address, :zip]
*
* Related: #to_a.
*/
static VALUE
rb_struct_members_m(VALUE obj)
{
return rb_struct_s_members_m(rb_obj_class(obj));
}
VALUE
rb_struct_getmember(VALUE obj, ID id)
{
......
#define rb_data_s_members_m rb_struct_s_members_m
/*
* call-seq:
* new(*args) -> instance
......
#define rb_data_to_h rb_struct_to_h
/*
* call-seq:
* members -> array_of_symbols
*
* Returns the member names from +self+ as an array:
*
* Measure = Data.define(:amount, :unit)
* distance = Measure[10, 'km']
*
* distance.members #=> [:amount, :unit]
*
*/
#define rb_data_members_m rb_struct_members_m
/*
* call-seq:
* deconstruct -> array
......
rb_define_method(rb_cStruct, "filter", rb_struct_select, -1);
rb_define_method(rb_cStruct, "values_at", rb_struct_values_at, -1);
rb_define_method(rb_cStruct, "members", rb_struct_members_m, 0);
rb_define_method(rb_cStruct, "dig", rb_struct_dig, -1);
rb_define_method(rb_cStruct, "deconstruct", rb_struct_to_a, 0);
......
rb_define_method(rb_cData, "inspect", rb_data_inspect, 0);
rb_define_alias(rb_cData, "to_s", "inspect");
rb_define_method(rb_cData, "to_h", rb_data_to_h, 0);
rb_define_method(rb_cData, "members", rb_data_members_m, 0);
rb_define_method(rb_cData, "to_h", rb_data_to_h, 0);
rb_define_method(rb_cData, "deconstruct", rb_data_deconstruct, 0);
rb_define_method(rb_cData, "deconstruct_keys", rb_data_deconstruct_keys, 1);
test/ruby/test_data.rb
test = klass.new(1, 2)
assert_equal(1, test.foo)
assert_equal(2, test.bar)
assert_equal(%i[foo bar], test.members)
assert_equal(1, test.public_send(:foo))
assert_equal(0, test.method(:foo).arity)
assert_equal([], test.method(:foo).parameters)
......
assert_not_equal(Data.define.new, test)
assert_equal('#<data >', test.inspect)
assert_equal([], test.members)
assert_equal({}, test.to_h)
end
test/ruby/test_struct.rb
def test_members
klass = @Struct.new(:a)
o = klass.new(1)
assert_equal([:a], klass.members)
assert_equal([:a], o.members)
assert_equal([:a], klass.members)
end
def test_ref
......
assert_nothing_raised { @Struct.new(:a) { } }
assert_raise(RuntimeError) { @Struct.new(:a) { raise } }
assert_equal([:utime, :stime, :cutime, :cstime], Process.times.members)
assert_equal([:utime, :stime, :cutime, :cstime], Process.times.class.members)
end
def test_struct_new_with_hash
(2-2/2)