1 /**********************************************************************
6 created at: Tue Mar 22 18:44:30 JST 1995
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
14 #include "internal/class.h"
15 #include "internal/error.h"
16 #include "internal/hash.h"
17 #include "internal/object.h"
18 #include "internal/proc.h"
19 #include "internal/struct.h"
20 #include "internal/symbol.h"
21 #include "transient_heap.h"
25 /* only for struct[:field] access */
28 AREF_HASH_THRESHOLD
= 10
32 static ID id_members
, id_back_members
, id_keyword_init
;
34 static VALUE
struct_alloc(VALUE
);
37 struct_ivar_get(VALUE c
, ID id
)
40 VALUE ivar
= rb_attr_get(c
, id
);
47 if (c
== 0 || c
== rb_cStruct
)
49 ivar
= rb_attr_get(c
, id
);
51 return rb_ivar_set(orig
, id
, ivar
);
57 rb_struct_s_keyword_init(VALUE klass
)
59 return struct_ivar_get(klass
, id_keyword_init
);
63 rb_struct_s_members(VALUE klass
)
65 VALUE members
= struct_ivar_get(klass
, id_members
);
68 rb_raise(rb_eTypeError
, "uninitialized struct");
70 if (!RB_TYPE_P(members
, T_ARRAY
)) {
71 rb_raise(rb_eTypeError
, "corrupted struct");
77 rb_struct_members(VALUE s
)
79 VALUE members
= rb_struct_s_members(rb_obj_class(s
));
81 if (RSTRUCT_LEN(s
) != RARRAY_LEN(members
)) {
82 rb_raise(rb_eTypeError
, "struct size differs (%ld required %ld given)",
83 RARRAY_LEN(members
), RSTRUCT_LEN(s
));
89 struct_member_pos_ideal(VALUE name
, long mask
)
91 /* (id & (mask/2)) * 2 */
92 return (SYM2ID(name
) >> (ID_SCOPE_SHIFT
- 1)) & mask
;
96 struct_member_pos_probe(long prev
, long mask
)
98 /* (((prev/2) * AREF_HASH_UNIT + 1) & (mask/2)) * 2 */
99 return (prev
* AREF_HASH_UNIT
+ 2) & mask
;
103 struct_set_members(VALUE klass
, VALUE
/* frozen hidden array */ members
)
106 const long members_length
= RARRAY_LEN(members
);
108 if (members_length
<= AREF_HASH_THRESHOLD
) {
112 long i
, j
, mask
= 64;
115 while (mask
< members_length
* AREF_HASH_UNIT
) mask
*= 2;
117 back
= rb_ary_tmp_new(mask
+ 1);
118 rb_ary_store(back
, mask
, INT2FIX(members_length
));
119 mask
-= 2; /* mask = (2**k-1)*2 */
121 for (i
=0; i
< members_length
; i
++) {
122 name
= RARRAY_AREF(members
, i
);
124 j
= struct_member_pos_ideal(name
, mask
);
127 if (!RTEST(RARRAY_AREF(back
, j
))) {
128 rb_ary_store(back
, j
, name
);
129 rb_ary_store(back
, j
+ 1, INT2FIX(i
));
132 j
= struct_member_pos_probe(j
, mask
);
135 OBJ_FREEZE_RAW(back
);
137 rb_ivar_set(klass
, id_members
, members
);
138 rb_ivar_set(klass
, id_back_members
, back
);
144 struct_member_pos(VALUE s
, VALUE name
)
146 VALUE back
= struct_ivar_get(rb_obj_class(s
), id_back_members
);
149 if (UNLIKELY(NIL_P(back
))) {
150 rb_raise(rb_eTypeError
, "uninitialized struct");
152 if (UNLIKELY(!RB_TYPE_P(back
, T_ARRAY
))) {
153 rb_raise(rb_eTypeError
, "corrupted struct");
156 mask
= RARRAY_LEN(back
);
158 if (mask
<= AREF_HASH_THRESHOLD
) {
159 if (UNLIKELY(RSTRUCT_LEN(s
) != mask
)) {
160 rb_raise(rb_eTypeError
,
161 "struct size differs (%ld required %ld given)",
162 mask
, RSTRUCT_LEN(s
));
164 for (j
= 0; j
< mask
; j
++) {
165 if (RARRAY_AREF(back
, j
) == name
)
171 if (UNLIKELY(RSTRUCT_LEN(s
) != FIX2INT(RARRAY_AREF(back
, mask
-1)))) {
172 rb_raise(rb_eTypeError
, "struct size differs (%d required %ld given)",
173 FIX2INT(RARRAY_AREF(back
, mask
-1)), RSTRUCT_LEN(s
));
177 j
= struct_member_pos_ideal(name
, mask
);
180 VALUE e
= RARRAY_AREF(back
, j
);
182 return FIX2INT(RARRAY_AREF(back
, j
+ 1));
186 j
= struct_member_pos_probe(j
, mask
);
192 * StructClass::members -> array_of_symbols
194 * Returns the member names of the Struct descendant as an array:
196 * Customer = Struct.new(:name, :address, :zip)
197 * Customer.members # => [:name, :address, :zip]
202 rb_struct_s_members_m(VALUE klass
)
204 VALUE members
= rb_struct_s_members(klass
);
206 return rb_ary_dup(members
);
211 * members -> array_of_symbols
213 * Returns the member names from +self+ as an array:
215 * Customer = Struct.new(:name, :address, :zip)
216 * Customer.new.members # => [:name, :address, :zip]
222 rb_struct_members_m(VALUE obj
)
224 return rb_struct_s_members_m(rb_obj_class(obj
));
228 rb_struct_getmember(VALUE obj
, ID id
)
230 VALUE slot
= ID2SYM(id
);
231 int i
= struct_member_pos(obj
, slot
);
233 return RSTRUCT_GET(obj
, i
);
235 rb_name_err_raise("`%1$s' is not a struct member", obj
, ID2SYM(id
));
237 UNREACHABLE_RETURN(Qnil
);
241 rb_struct_modify(VALUE s
)
247 anonymous_struct(VALUE klass
)
251 nstr
= rb_class_new(klass
);
252 rb_make_metaclass(nstr
, RBASIC(klass
)->klass
);
253 rb_class_inherited(klass
, nstr
);
258 new_struct(VALUE name
, VALUE super
)
260 /* old style: should we warn? */
262 name
= rb_str_to_str(name
);
263 if (!rb_is_const_name(name
)) {
264 rb_name_err_raise("identifier %1$s needs to be constant",
268 if (rb_const_defined_at(super
, id
)) {
269 rb_warn("redefining constant %"PRIsVALUE
"::%"PRIsVALUE
, super
, name
);
270 rb_mod_remove_const(super
, ID2SYM(id
));
272 return rb_define_class_id_under(super
, id
, super
);
275 NORETURN(static void invalid_struct_pos(VALUE s
, VALUE idx
));
278 define_aref_method(VALUE nstr
, VALUE name
, VALUE off
)
280 rb_add_method_optimized(nstr
, SYM2ID(name
), OPTIMIZED_METHOD_TYPE_STRUCT_AREF
, FIX2UINT(off
), METHOD_VISI_PUBLIC
);
284 define_aset_method(VALUE nstr
, VALUE name
, VALUE off
)
286 rb_add_method_optimized(nstr
, SYM2ID(name
), OPTIMIZED_METHOD_TYPE_STRUCT_ASET
, FIX2UINT(off
), METHOD_VISI_PUBLIC
);
290 rb_struct_s_inspect(VALUE klass
)
292 VALUE inspect
= rb_class_name(klass
);
293 if (RTEST(rb_struct_s_keyword_init(klass
))) {
294 rb_str_cat_cstr(inspect
, "(keyword_init: true)");
303 * StructClass::keyword_init? -> true or falsy value
305 * Returns +true+ if the class was initialized with <tt>keyword_init: true</tt>.
306 * Otherwise returns +nil+ or +false+.
309 * Foo = Struct.new(:a)
310 * Foo.keyword_init? # => nil
311 * Bar = Struct.new(:a, keyword_init: true)
312 * Bar.keyword_init? # => true
313 * Baz = Struct.new(:a, keyword_init: false)
314 * Baz.keyword_init? # => false
317 rb_struct_s_keyword_init_p(VALUE obj
)
322 #define rb_struct_s_keyword_init_p rb_struct_s_keyword_init
325 setup_struct(VALUE nstr
, VALUE members
)
329 members
= struct_set_members(nstr
, members
);
331 rb_define_alloc_func(nstr
, struct_alloc
);
332 rb_define_singleton_method(nstr
, "new", rb_class_new_instance_pass_kw
, -1);
333 rb_define_singleton_method(nstr
, "[]", rb_class_new_instance_pass_kw
, -1);
334 rb_define_singleton_method(nstr
, "members", rb_struct_s_members_m
, 0);
335 rb_define_singleton_method(nstr
, "inspect", rb_struct_s_inspect
, 0);
336 rb_define_singleton_method(nstr
, "keyword_init?", rb_struct_s_keyword_init_p
, 0);
338 len
= RARRAY_LEN(members
);
339 for (i
=0; i
< len
; i
++) {
340 VALUE sym
= RARRAY_AREF(members
, i
);
342 VALUE off
= LONG2NUM(i
);
344 define_aref_method(nstr
, sym
, off
);
345 define_aset_method(nstr
, ID2SYM(rb_id_attrset(id
)), off
);
352 rb_struct_alloc_noinit(VALUE klass
)
354 return struct_alloc(klass
);
358 struct_make_members_list(va_list ar
)
361 VALUE ary
, list
= rb_ident_hash_new();
362 st_table
*tbl
= RHASH_TBL_RAW(list
);
364 RBASIC_CLEAR_CLASS(list
);
365 OBJ_WB_UNPROTECT(list
);
366 while ((mem
= va_arg(ar
, char*)) != 0) {
367 VALUE sym
= rb_sym_intern_ascii_cstr(mem
);
368 if (st_insert(tbl
, sym
, Qtrue
)) {
369 rb_raise(rb_eArgError
, "duplicate member: %s", mem
);
372 ary
= rb_hash_keys(list
);
374 RBASIC_CLEAR_CLASS(ary
);
380 struct_define_without_accessor(VALUE outer
, const char *class_name
, VALUE super
, rb_alloc_func_t alloc
, VALUE members
)
386 klass
= rb_define_class_under(outer
, class_name
, super
);
389 klass
= rb_define_class(class_name
, super
);
393 klass
= anonymous_struct(super
);
396 struct_set_members(klass
, members
);
399 rb_define_alloc_func(klass
, alloc
);
402 rb_define_alloc_func(klass
, struct_alloc
);
409 rb_struct_define_without_accessor_under(VALUE outer
, const char *class_name
, VALUE super
, rb_alloc_func_t alloc
, ...)
415 members
= struct_make_members_list(ar
);
418 return struct_define_without_accessor(outer
, class_name
, super
, alloc
, members
);
422 rb_struct_define_without_accessor(const char *class_name
, VALUE super
, rb_alloc_func_t alloc
, ...)
428 members
= struct_make_members_list(ar
);
431 return struct_define_without_accessor(0, class_name
, super
, alloc
, members
);
435 rb_struct_define(const char *name
, ...)
441 ary
= struct_make_members_list(ar
);
444 if (!name
) st
= anonymous_struct(rb_cStruct
);
445 else st
= new_struct(rb_str_new2(name
), rb_cStruct
);
446 return setup_struct(st
, ary
);
450 rb_struct_define_under(VALUE outer
, const char *name
, ...)
456 ary
= struct_make_members_list(ar
);
459 return setup_struct(rb_define_class_under(outer
, name
, rb_cStruct
), ary
);
464 * Struct.new(*member_names, keyword_init: false){|Struct_subclass| ... } -> Struct_subclass
465 * Struct.new(class_name, *member_names, keyword_init: false){|Struct_subclass| ... } -> Struct_subclass
466 * Struct_subclass.new(*member_names) -> Struct_subclass_instance
467 * Struct_subclass.new(**member_names) -> Struct_subclass_instance
469 * <tt>Struct.new</tt> returns a new subclass of +Struct+. The new subclass:
471 * - May be anonymous, or may have the name given by +class_name+.
472 * - May have members as given by +member_names+.
473 * - May have initialization via ordinary arguments (the default)
474 * or via keyword arguments (if <tt>keyword_init: true</tt> is given).
476 * The new subclass has its own method <tt>::new</tt>; thus:
478 * Foo = Struct.new('Foo', :foo, :bar) # => Struct::Foo
479 * f = Foo.new(0, 1) # => #<struct Struct::Foo foo=0, bar=1>
483 * With string argument +class_name+,
484 * returns a new subclass of +Struct+ named <tt>Struct::<em>class_name</em></tt>:
486 * Foo = Struct.new('Foo', :foo, :bar) # => Struct::Foo
487 * Foo.name # => "Struct::Foo"
488 * Foo.superclass # => Struct
490 * Without string argument +class_name+,
491 * returns a new anonymous subclass of +Struct+:
493 * Struct.new(:foo, :bar).name # => nil
497 * With a block given, the created subclass is yielded to the block:
499 * Customer = Struct.new('Customer', :name, :address) do |new_class|
500 * p "The new subclass is #{new_class}"
502 * "Hello #{name} at #{address}"
504 * end # => Struct::Customer
505 * dave = Customer.new('Dave', '123 Main')
506 * dave # => #<struct Struct::Customer name="Dave", address="123 Main">
507 * dave.greeting # => "Hello Dave at 123 Main"
509 * Output, from <tt>Struct.new</tt>:
511 * "The new subclass is Struct::Customer"
513 * <b>Member Names</b>
515 * \Symbol arguments +member_names+
516 * determines the members of the new subclass:
518 * Struct.new(:foo, :bar).members # => [:foo, :bar]
519 * Struct.new('Foo', :foo, :bar).members # => [:foo, :bar]
521 * The new subclass has instance methods corresponding to +member_names+:
523 * Foo = Struct.new('Foo', :foo, :bar)
524 * Foo.instance_methods(false) # => [:foo, :bar, :foo=, :bar=]
525 * f = Foo.new # => #<struct Struct::Foo foo=nil, bar=nil>
530 * f # => #<struct Struct::Foo foo=0, bar=1>
532 * <b>Singleton Methods</b>
534 * A subclass returned by Struct.new has these singleton methods:
536 * - \Method <tt>::new </tt> creates an instance of the subclass:
538 * Foo.new # => #<struct Struct::Foo foo=nil, bar=nil>
539 * Foo.new(0) # => #<struct Struct::Foo foo=0, bar=nil>
540 * Foo.new(0, 1) # => #<struct Struct::Foo foo=0, bar=1>
541 * Foo.new(0, 1, 2) # Raises ArgumentError: struct size differs
543 * \Method <tt>::[]</tt> is an alias for method <tt>::new</tt>.
545 * - \Method <tt>:inspect</tt> returns a string representation of the subclass:
550 * - \Method <tt>::members</tt> returns an array of the member names:
552 * Foo.members # => [:foo, :bar]
554 * <b>Keyword Argument</b>
556 * By default, the arguments for initializing an instance of the new subclass
557 * are ordinary arguments (not keyword arguments).
558 * With optional keyword argument <tt>keyword_init: true</tt>,
559 * the new subclass is initialized with keyword arguments:
561 * # Without keyword_init: true.
562 * Foo = Struct.new('Foo', :foo, :bar)
563 * Foo # => Struct::Foo
564 * Foo.new(0, 1) # => #<struct Struct::Foo foo=0, bar=1>
565 * # With keyword_init: true.
566 * Bar = Struct.new(:foo, :bar, keyword_init: true)
567 * Bar # => # => Bar(keyword_init: true)
568 * Bar.new(bar: 1, foo: 0) # => #<struct Bar foo=0, bar=1>
573 rb_struct_s_def(int argc
, VALUE
*argv
, VALUE klass
)
575 VALUE name
, rest
, keyword_init
= Qnil
;
580 rb_check_arity(argc
, 1, UNLIMITED_ARGUMENTS
);
582 if (SYMBOL_P(name
)) {
590 if (RB_TYPE_P(argv
[argc
-1], T_HASH
)) {
591 static ID keyword_ids
[1];
593 if (!keyword_ids
[0]) {
594 keyword_ids
[0] = rb_intern("keyword_init");
596 rb_get_kwargs(argv
[argc
-1], keyword_ids
, 0, 1, &keyword_init
);
597 if (keyword_init
== Qundef
) {
600 else if (RTEST(keyword_init
)) {
601 keyword_init
= Qtrue
;
606 rest
= rb_ident_hash_new();
607 RBASIC_CLEAR_CLASS(rest
);
608 OBJ_WB_UNPROTECT(rest
);
609 tbl
= RHASH_TBL_RAW(rest
);
610 for (i
=0; i
<argc
; i
++) {
611 VALUE mem
= rb_to_symbol(argv
[i
]);
612 if (rb_is_attrset_sym(mem
)) {
613 rb_raise(rb_eArgError
, "invalid struct member: %"PRIsVALUE
, mem
);
615 if (st_insert(tbl
, mem
, Qtrue
)) {
616 rb_raise(rb_eArgError
, "duplicate member: %"PRIsVALUE
, mem
);
619 rest
= rb_hash_keys(rest
);
621 RBASIC_CLEAR_CLASS(rest
);
622 OBJ_FREEZE_RAW(rest
);
624 st
= anonymous_struct(klass
);
627 st
= new_struct(name
, klass
);
629 setup_struct(st
, rest
);
630 rb_ivar_set(st
, id_keyword_init
, keyword_init
);
631 if (rb_block_given_p()) {
632 rb_mod_module_eval(0, 0, st
);
639 num_members(VALUE klass
)
642 members
= struct_ivar_get(klass
, id_members
);
643 if (!RB_TYPE_P(members
, T_ARRAY
)) {
644 rb_raise(rb_eTypeError
, "broken members");
646 return RARRAY_LEN(members
);
652 struct struct_hash_set_arg
{
654 VALUE unknown_keywords
;
657 static int rb_struct_pos(VALUE s
, VALUE
*name
);
660 struct_hash_set_i(VALUE key
, VALUE val
, VALUE arg
)
662 struct struct_hash_set_arg
*args
= (struct struct_hash_set_arg
*)arg
;
663 int i
= rb_struct_pos(args
->self
, &key
);
665 if (NIL_P(args
->unknown_keywords
)) {
666 args
->unknown_keywords
= rb_ary_new();
668 rb_ary_push(args
->unknown_keywords
, key
);
671 rb_struct_modify(args
->self
);
672 RSTRUCT_SET(args
->self
, i
, val
);
678 rb_struct_initialize_m(int argc
, const VALUE
*argv
, VALUE self
)
680 VALUE klass
= rb_obj_class(self
);
681 rb_struct_modify(self
);
682 long n
= num_members(klass
);
684 rb_mem_clear((VALUE
*)RSTRUCT_CONST_PTR(self
), n
);
688 VALUE keyword_init
= rb_struct_s_keyword_init(klass
);
689 if (RTEST(keyword_init
)) {
690 struct struct_hash_set_arg arg
;
691 if (argc
> 1 || !RB_TYPE_P(argv
[0], T_HASH
)) {
692 rb_raise(rb_eArgError
, "wrong number of arguments (given %d, expected 0)", argc
);
694 rb_mem_clear((VALUE
*)RSTRUCT_CONST_PTR(self
), n
);
696 arg
.unknown_keywords
= Qnil
;
697 rb_hash_foreach(argv
[0], struct_hash_set_i
, (VALUE
)&arg
);
698 if (arg
.unknown_keywords
!= Qnil
) {
699 rb_raise(rb_eArgError
, "unknown keywords: %s",
700 RSTRING_PTR(rb_ary_join(arg
.unknown_keywords
, rb_str_new2(", "))));
705 rb_raise(rb_eArgError
, "struct size differs");
707 if (NIL_P(keyword_init
) && argc
== 1 && RB_TYPE_P(argv
[0], T_HASH
) && rb_keyword_given_p()) {
708 rb_warn("Passing only keyword arguments to Struct#initialize will behave differently from Ruby 3.2. "\
709 "Please use a Hash literal like .new({k: v}) instead of .new(k: v).");
711 for (long i
=0; i
<argc
; i
++) {
712 RSTRUCT_SET(self
, i
, argv
[i
]);
715 rb_mem_clear((VALUE
*)RSTRUCT_CONST_PTR(self
)+argc
, n
-argc
);
722 rb_struct_initialize(VALUE self
, VALUE values
)
724 rb_struct_initialize_m(RARRAY_LENINT(values
), RARRAY_CONST_PTR(values
), self
);
730 struct_heap_alloc(VALUE st
, size_t len
)
732 VALUE
*ptr
= rb_transient_heap_alloc((VALUE
)st
, sizeof(VALUE
) * len
);
735 RSTRUCT_TRANSIENT_SET(st
);
739 RSTRUCT_TRANSIENT_UNSET(st
);
740 return ALLOC_N(VALUE
, len
);
744 #if USE_TRANSIENT_HEAP
746 rb_struct_transient_heap_evacuate(VALUE obj
, int promote
)
748 if (RSTRUCT_TRANSIENT_P(obj
)) {
749 const VALUE
*old_ptr
= rb_struct_const_heap_ptr(obj
);
751 long len
= RSTRUCT_LEN(obj
);
754 new_ptr
= ALLOC_N(VALUE
, len
);
755 FL_UNSET_RAW(obj
, RSTRUCT_TRANSIENT_FLAG
);
758 new_ptr
= struct_heap_alloc(obj
, len
);
760 MEMCPY(new_ptr
, old_ptr
, VALUE
, len
);
761 RSTRUCT(obj
)->as
.heap
.ptr
= new_ptr
;
767 struct_alloc(VALUE klass
)
770 NEWOBJ_OF(st
, struct RStruct
, klass
, T_STRUCT
| (RGENGC_WB_PROTECTED_STRUCT
? FL_WB_PROTECTED
: 0));
772 n
= num_members(klass
);
774 if (0 < n
&& n
<= RSTRUCT_EMBED_LEN_MAX
) {
775 RBASIC(st
)->flags
&= ~RSTRUCT_EMBED_LEN_MASK
;
776 RBASIC(st
)->flags
|= n
<< RSTRUCT_EMBED_LEN_SHIFT
;
777 rb_mem_clear((VALUE
*)st
->as
.ary
, n
);
780 st
->as
.heap
.ptr
= struct_heap_alloc((VALUE
)st
, n
);
781 rb_mem_clear((VALUE
*)st
->as
.heap
.ptr
, n
);
789 rb_struct_alloc(VALUE klass
, VALUE values
)
791 return rb_class_new_instance(RARRAY_LENINT(values
), RARRAY_CONST_PTR(values
), klass
);
795 rb_struct_new(VALUE klass
, ...)
797 VALUE tmpargs
[16], *mem
= tmpargs
;
801 size
= rb_long2int(num_members(klass
));
802 if (size
> numberof(tmpargs
)) {
803 tmpargs
[0] = rb_ary_tmp_new(size
);
804 mem
= RARRAY_PTR(tmpargs
[0]);
806 va_start(args
, klass
);
807 for (i
=0; i
<size
; i
++) {
808 mem
[i
] = va_arg(args
, VALUE
);
812 return rb_class_new_instance(size
, mem
, klass
);
816 struct_enum_size(VALUE s
, VALUE args
, VALUE eobj
)
818 return rb_struct_size(s
);
823 * each {|value| ... } -> self
826 * Calls the given block with the value of each member; returns +self+:
828 * Customer = Struct.new(:name, :address, :zip)
829 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
830 * joe.each {|value| p value }
835 * "123 Maple, Anytown NC"
838 * Returns an Enumerator if no block is given.
840 * Related: #each_pair.
844 rb_struct_each(VALUE s
)
848 RETURN_SIZED_ENUMERATOR(s
, 0, 0, struct_enum_size
);
849 for (i
=0; i
<RSTRUCT_LEN(s
); i
++) {
850 rb_yield(RSTRUCT_GET(s
, i
));
857 * each_pair {|(name, value)| ... } -> self
858 * each_pair -> enumerator
860 * Calls the given block with each member name/value pair; returns +self+:
862 * Customer = Struct.new(:name, :address, :zip) # => Customer
863 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
864 * joe.each_pair {|(name, value)| p "#{name} => #{value}" }
868 * "name => Joe Smith"
869 * "address => 123 Maple, Anytown NC"
872 * Returns an Enumerator if no block is given.
879 rb_struct_each_pair(VALUE s
)
884 RETURN_SIZED_ENUMERATOR(s
, 0, 0, struct_enum_size
);
885 members
= rb_struct_members(s
);
886 if (rb_block_pair_yield_optimizable()) {
887 for (i
=0; i
<RSTRUCT_LEN(s
); i
++) {
888 VALUE key
= rb_ary_entry(members
, i
);
889 VALUE value
= RSTRUCT_GET(s
, i
);
890 rb_yield_values(2, key
, value
);
894 for (i
=0; i
<RSTRUCT_LEN(s
); i
++) {
895 VALUE key
= rb_ary_entry(members
, i
);
896 VALUE value
= RSTRUCT_GET(s
, i
);
897 rb_yield(rb_assoc_new(key
, value
));
904 inspect_struct(VALUE s
, VALUE dummy
, int recur
)
906 VALUE cname
= rb_class_path(rb_obj_class(s
));
907 VALUE members
, str
= rb_str_new2("#<struct ");
909 char first
= RSTRING_PTR(cname
)[0];
911 if (recur
|| first
!= '#') {
912 rb_str_append(str
, cname
);
915 return rb_str_cat2(str
, ":...>");
918 members
= rb_struct_members(s
);
919 len
= RSTRUCT_LEN(s
);
921 for (i
=0; i
<len
; i
++) {
926 rb_str_cat2(str
, ", ");
928 else if (first
!= '#') {
929 rb_str_cat2(str
, " ");
931 slot
= RARRAY_AREF(members
, i
);
933 if (rb_is_local_id(id
) || rb_is_const_id(id
)) {
934 rb_str_append(str
, rb_id2str(id
));
937 rb_str_append(str
, rb_inspect(slot
));
939 rb_str_cat2(str
, "=");
940 rb_str_append(str
, rb_inspect(RSTRUCT_GET(s
, i
)));
942 rb_str_cat2(str
, ">");
951 * Returns a string representation of +self+:
953 * Customer = Struct.new(:name, :address, :zip) # => Customer
954 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
955 * joe.inspect # => "#<struct Customer name=\"Joe Smith\", address=\"123 Maple, Anytown NC\", zip=12345>"
957 * Struct#to_s is an alias for Struct#inspect.
962 rb_struct_inspect(VALUE s
)
964 return rb_exec_recursive(inspect_struct
, s
, 0);
971 * Returns the values in +self+ as an array:
973 * Customer = Struct.new(:name, :address, :zip)
974 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
975 * joe.to_a # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
977 * Struct#values and Struct#deconstruct are aliases for Struct#to_a.
983 rb_struct_to_a(VALUE s
)
985 return rb_ary_new4(RSTRUCT_LEN(s
), RSTRUCT_CONST_PTR(s
));
991 * to_h {|name, value| ... } -> hash
993 * Returns a hash containing the name and value for each member:
995 * Customer = Struct.new(:name, :address, :zip)
996 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
998 * h # => {:name=>"Joe Smith", :address=>"123 Maple, Anytown NC", :zip=>12345}
1000 * If a block is given, it is called with each name/value pair;
1001 * the block should return a 2-element array whose elements will become
1002 * a key/value pair in the returned hash:
1004 * h = joe.to_h{|name, value| [name.upcase, value.to_s.upcase]}
1005 * h # => {:NAME=>"JOE SMITH", :ADDRESS=>"123 MAPLE, ANYTOWN NC", :ZIP=>"12345"}
1007 * Raises ArgumentError if the block returns an inappropriate value.
1012 rb_struct_to_h(VALUE s
)
1014 VALUE h
= rb_hash_new_with_size(RSTRUCT_LEN(s
));
1015 VALUE members
= rb_struct_members(s
);
1017 int block_given
= rb_block_given_p();
1019 for (i
=0; i
<RSTRUCT_LEN(s
); i
++) {
1020 VALUE k
= rb_ary_entry(members
, i
), v
= RSTRUCT_GET(s
, i
);
1022 rb_hash_set_pair(h
, rb_yield_values(2, k
, v
));
1024 rb_hash_aset(h
, k
, v
);
1031 * deconstruct_keys(array_of_names) -> hash
1033 * Returns a hash of the name/value pairs for the given member names.
1035 * Customer = Struct.new(:name, :address, :zip)
1036 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1037 * h = joe.deconstruct_keys([:zip, :address])
1038 * h # => {:zip=>12345, :address=>"123 Maple, Anytown NC"}
1040 * Returns all names and values if +array_of_names+ is +nil+:
1042 * h = joe.deconstruct_keys(nil)
1043 * h # => {:name=>"Joseph Smith, Jr.", :address=>"123 Maple, Anytown NC", :zip=>12345}
1047 rb_struct_deconstruct_keys(VALUE s
, VALUE keys
)
1053 return rb_struct_to_h(s
);
1055 if (UNLIKELY(!RB_TYPE_P(keys
, T_ARRAY
))) {
1056 rb_raise(rb_eTypeError
,
1057 "wrong argument type %"PRIsVALUE
" (expected Array or nil)",
1058 rb_obj_class(keys
));
1061 if (RSTRUCT_LEN(s
) < RARRAY_LEN(keys
)) {
1062 return rb_hash_new_with_size(0);
1064 h
= rb_hash_new_with_size(RARRAY_LEN(keys
));
1065 for (i
=0; i
<RARRAY_LEN(keys
); i
++) {
1066 VALUE key
= RARRAY_AREF(keys
, i
);
1067 int i
= rb_struct_pos(s
, &key
);
1071 rb_hash_aset(h
, key
, RSTRUCT_GET(s
, i
));
1078 rb_struct_init_copy(VALUE copy
, VALUE s
)
1082 if (!OBJ_INIT_COPY(copy
, s
)) return copy
;
1083 if (RSTRUCT_LEN(copy
) != RSTRUCT_LEN(s
)) {
1084 rb_raise(rb_eTypeError
, "struct size mismatch");
1087 for (i
=0, len
=RSTRUCT_LEN(copy
); i
<len
; i
++) {
1088 RSTRUCT_SET(copy
, i
, RSTRUCT_GET(s
, i
));
1095 rb_struct_pos(VALUE s
, VALUE
*name
)
1100 if (SYMBOL_P(idx
)) {
1101 return struct_member_pos(s
, idx
);
1103 else if (RB_TYPE_P(idx
, T_STRING
)) {
1104 idx
= rb_check_symbol(name
);
1105 if (NIL_P(idx
)) return -1;
1106 return struct_member_pos(s
, idx
);
1111 len
= RSTRUCT_LEN(s
);
1114 *name
= LONG2FIX(i
);
1119 else if (len
<= i
) {
1120 *name
= LONG2FIX(i
);
1128 invalid_struct_pos(VALUE s
, VALUE idx
)
1130 if (FIXNUM_P(idx
)) {
1131 long i
= FIX2INT(idx
), len
= RSTRUCT_LEN(s
);
1133 rb_raise(rb_eIndexError
, "offset %ld too small for struct(size:%ld)",
1137 rb_raise(rb_eIndexError
, "offset %ld too large for struct(size:%ld)",
1142 rb_name_err_raise("no member '%1$s' in struct", s
, idx
);
1148 * struct[name] -> object
1149 * struct[n] -> object
1151 * Returns a value from +self+.
1153 * With symbol or string argument +name+ given, returns the value for the named member:
1155 * Customer = Struct.new(:name, :address, :zip)
1156 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1157 * joe[:zip] # => 12345
1159 * Raises NameError if +name+ is not the name of a member.
1161 * With integer argument +n+ given, returns <tt>self.values[n]</tt>
1162 * if +n+ is in range;
1163 * see {Array Indexes}[Array.html#class-Array-label-Array+Indexes]:
1166 * joe[-2] # => "123 Maple, Anytown NC"
1168 * Raises IndexError if +n+ is out of range.
1173 rb_struct_aref(VALUE s
, VALUE idx
)
1175 int i
= rb_struct_pos(s
, &idx
);
1176 if (i
< 0) invalid_struct_pos(s
, idx
);
1177 return RSTRUCT_GET(s
, i
);
1182 * struct[name] = value -> value
1183 * struct[n] = value -> value
1185 * Assigns a value to a member.
1187 * With symbol or string argument +name+ given, assigns the given +value+
1188 * to the named member; returns +value+:
1190 * Customer = Struct.new(:name, :address, :zip)
1191 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1192 * joe[:zip] = 54321 # => 54321
1193 * joe # => #<struct Customer name="Joe Smith", address="123 Maple, Anytown NC", zip=54321>
1195 * Raises NameError if +name+ is not the name of a member.
1197 * With integer argument +n+ given, assigns the given +value+
1198 * to the +n+-th member if +n+ is in range;
1199 * see {Array Indexes}[Array.html#class-Array-label-Array+Indexes]:
1201 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1202 * joe[2] = 54321 # => 54321
1203 * joe[-3] = 'Joseph Smith' # => "Joseph Smith"
1204 * joe # => #<struct Customer name="Joseph Smith", address="123 Maple, Anytown NC", zip=54321>
1206 * Raises IndexError if +n+ is out of range.
1211 rb_struct_aset(VALUE s
, VALUE idx
, VALUE val
)
1213 int i
= rb_struct_pos(s
, &idx
);
1214 if (i
< 0) invalid_struct_pos(s
, idx
);
1215 rb_struct_modify(s
);
1216 RSTRUCT_SET(s
, i
, val
);
1220 FUNC_MINIMIZED(VALUE
rb_struct_lookup(VALUE s
, VALUE idx
));
1221 NOINLINE(static VALUE
rb_struct_lookup_default(VALUE s
, VALUE idx
, VALUE notfound
));
1224 rb_struct_lookup(VALUE s
, VALUE idx
)
1226 return rb_struct_lookup_default(s
, idx
, Qnil
);
1230 rb_struct_lookup_default(VALUE s
, VALUE idx
, VALUE notfound
)
1232 int i
= rb_struct_pos(s
, &idx
);
1233 if (i
< 0) return notfound
;
1234 return RSTRUCT_GET(s
, i
);
1238 struct_entry(VALUE s
, long n
)
1240 return rb_struct_aref(s
, LONG2NUM(n
));
1245 * values_at(*integers) -> array
1246 * values_at(integer_range) -> array
1248 * Returns an array of values from +self+.
1250 * With integer arguments +integers+ given,
1251 * returns an array containing each value given by one of +integers+:
1253 * Customer = Struct.new(:name, :address, :zip)
1254 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1255 * joe.values_at(0, 2) # => ["Joe Smith", 12345]
1256 * joe.values_at(2, 0) # => [12345, "Joe Smith"]
1257 * joe.values_at(2, 1, 0) # => [12345, "123 Maple, Anytown NC", "Joe Smith"]
1258 * joe.values_at(0, -3) # => ["Joe Smith", "Joe Smith"]
1260 * Raises IndexError if any of +integers+ is out of range;
1261 * see {Array Indexes}[Array.html#class-Array-label-Array+Indexes].
1263 * With integer range argument +integer_range+ given,
1264 * returns an array containing each value given by the elements of the range;
1265 * fills with +nil+ values for range elements larger than the structure:
1267 * joe.values_at(0..2)
1268 * # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
1269 * joe.values_at(-3..-1)
1270 * # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
1271 * joe.values_at(1..4) # => ["123 Maple, Anytown NC", 12345, nil, nil]
1273 * Raises RangeError if any element of the range is negative and out of range;
1274 * see {Array Indexes}[Array.html#class-Array-label-Array+Indexes].
1279 rb_struct_values_at(int argc
, VALUE
*argv
, VALUE s
)
1281 return rb_get_values_at(s
, RSTRUCT_LEN(s
), argc
, argv
, struct_entry
);
1286 * select {|value| ... } -> array
1287 * select -> enumerator
1289 * With a block given, returns an array of values from +self+
1290 * for which the block returns a truthy value:
1292 * Customer = Struct.new(:name, :address, :zip)
1293 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1294 * a = joe.select {|value| value.is_a?(String) }
1295 * a # => ["Joe Smith", "123 Maple, Anytown NC"]
1296 * a = joe.select {|value| value.is_a?(Integer) }
1299 * With no block given, returns an Enumerator.
1301 * Struct#filter is an alias for Struct#select.
1305 rb_struct_select(int argc
, VALUE
*argv
, VALUE s
)
1310 rb_check_arity(argc
, 0, 0);
1311 RETURN_SIZED_ENUMERATOR(s
, 0, 0, struct_enum_size
);
1312 result
= rb_ary_new();
1313 for (i
= 0; i
< RSTRUCT_LEN(s
); i
++) {
1314 if (RTEST(rb_yield(RSTRUCT_GET(s
, i
)))) {
1315 rb_ary_push(result
, RSTRUCT_GET(s
, i
));
1323 recursive_equal(VALUE s
, VALUE s2
, int recur
)
1327 if (recur
) return Qtrue
; /* Subtle! */
1328 len
= RSTRUCT_LEN(s
);
1329 for (i
=0; i
<len
; i
++) {
1330 if (!rb_equal(RSTRUCT_GET(s
, i
), RSTRUCT_GET(s2
, i
))) return Qfalse
;
1338 * self == other -> true or false
1340 * Returns +true+ if and only if the following are true; otherwise returns +false+:
1342 * - <tt>other.class == self.class</tt>.
1343 * - For each member name +name+, <tt>other.name == self.name</tt>.
1347 * Customer = Struct.new(:name, :address, :zip)
1348 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1349 * joe_jr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1350 * joe_jr == joe # => true
1351 * joe_jr[:name] = 'Joe Smith, Jr.'
1352 * # => "Joe Smith, Jr."
1353 * joe_jr == joe # => false
1357 rb_struct_equal(VALUE s
, VALUE s2
)
1359 if (s
== s2
) return Qtrue
;
1360 if (!RB_TYPE_P(s2
, T_STRUCT
)) return Qfalse
;
1361 if (rb_obj_class(s
) != rb_obj_class(s2
)) return Qfalse
;
1362 if (RSTRUCT_LEN(s
) != RSTRUCT_LEN(s2
)) {
1363 rb_bug("inconsistent struct"); /* should never happen */
1366 return rb_exec_recursive_paired(recursive_equal
, s
, s2
, s2
);
1373 * Returns the integer hash value for +self+.
1375 * Two structs of the same class and with the same content
1376 * will have the same hash code (and will compare using Struct#eql?):
1378 * Customer = Struct.new(:name, :address, :zip)
1379 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1380 * joe_jr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1381 * joe.hash == joe_jr.hash # => true
1382 * joe_jr[:name] = 'Joe Smith, Jr.'
1383 * joe.hash == joe_jr.hash # => false
1385 * Related: Object#hash.
1389 rb_struct_hash(VALUE s
)
1395 h
= rb_hash_start(rb_hash(rb_obj_class(s
)));
1396 len
= RSTRUCT_LEN(s
);
1397 for (i
= 0; i
< len
; i
++) {
1398 n
= rb_hash(RSTRUCT_GET(s
, i
));
1399 h
= rb_hash_uint(h
, NUM2LONG(n
));
1406 recursive_eql(VALUE s
, VALUE s2
, int recur
)
1410 if (recur
) return Qtrue
; /* Subtle! */
1411 len
= RSTRUCT_LEN(s
);
1412 for (i
=0; i
<len
; i
++) {
1413 if (!rb_eql(RSTRUCT_GET(s
, i
), RSTRUCT_GET(s2
, i
))) return Qfalse
;
1420 * eql?(other) -> true or false
1422 * Returns +true+ if and only if the following are true; otherwise returns +false+:
1424 * - <tt>other.class == self.class</tt>.
1425 * - For each member name +name+, <tt>other.name.eql?(self.name)</tt>.
1427 * Customer = Struct.new(:name, :address, :zip)
1428 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1429 * joe_jr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1430 * joe_jr.eql?(joe) # => true
1431 * joe_jr[:name] = 'Joe Smith, Jr.'
1432 * joe_jr.eql?(joe) # => false
1434 * Related: Object#==.
1438 rb_struct_eql(VALUE s
, VALUE s2
)
1440 if (s
== s2
) return Qtrue
;
1441 if (!RB_TYPE_P(s2
, T_STRUCT
)) return Qfalse
;
1442 if (rb_obj_class(s
) != rb_obj_class(s2
)) return Qfalse
;
1443 if (RSTRUCT_LEN(s
) != RSTRUCT_LEN(s2
)) {
1444 rb_bug("inconsistent struct"); /* should never happen */
1447 return rb_exec_recursive_paired(recursive_eql
, s
, s2
, s2
);
1454 * Returns the number of members.
1456 * Customer = Struct.new(:name, :address, :zip)
1457 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1460 * Struct#length is an alias for Struct#size.
1464 rb_struct_size(VALUE s
)
1466 return LONG2FIX(RSTRUCT_LEN(s
));
1471 * dig(name, *identifiers) -> object
1472 * dig(n, *identifiers) -> object
1474 * Finds and returns an object among nested objects.
1475 * The nested objects may be instances of various classes.
1476 * See {Dig Methods}[rdoc-ref:dig_methods.rdoc].
1479 * Given symbol or string argument +name+,
1480 * returns the object that is specified by +name+ and +identifiers+:
1482 * Foo = Struct.new(:a)
1483 * f = Foo.new(Foo.new({b: [1, 2, 3]}))
1484 * f.dig(:a) # => #<struct Foo a={:b=>[1, 2, 3]}>
1485 * f.dig(:a, :a) # => {:b=>[1, 2, 3]}
1486 * f.dig(:a, :a, :b) # => [1, 2, 3]
1487 * f.dig(:a, :a, :b, 0) # => 1
1488 * f.dig(:b, 0) # => nil
1490 * Given integer argument +n+,
1491 * returns the object that is specified by +n+ and +identifiers+:
1493 * f.dig(0) # => #<struct Foo a={:b=>[1, 2, 3]}>
1494 * f.dig(0, 0) # => {:b=>[1, 2, 3]}
1495 * f.dig(0, 0, :b) # => [1, 2, 3]
1496 * f.dig(0, 0, :b, 0) # => 1
1497 * f.dig(:b, 0) # => nil
1502 rb_struct_dig(int argc
, VALUE
*argv
, VALUE self
)
1504 rb_check_arity(argc
, 1, UNLIMITED_ARGUMENTS
);
1505 self
= rb_struct_lookup(self
, *argv
);
1506 if (!--argc
) return self
;
1508 return rb_obj_dig(argc
, argv
, self
, Qnil
);
1512 * Document-class: Struct
1514 * \Class \Struct provides a convenient way to create a simple class
1515 * that can store and fetch values.
1517 * This example creates a subclass of +Struct+, <tt>Struct::Customer</tt>;
1518 * the first argument, a string, is the name of the subclass;
1519 * the other arguments, symbols, determine the _members_ of the new subclass.
1521 * Customer = Struct.new('Customer', :name, :address, :zip)
1522 * Customer.name # => "Struct::Customer"
1523 * Customer.class # => Class
1524 * Customer.superclass # => Struct
1526 * Corresponding to each member are two methods, a writer and a reader,
1527 * that store and fetch values:
1529 * methods = Customer.instance_methods false
1530 * methods # => [:zip, :address=, :zip=, :address, :name, :name=]
1532 * An instance of the subclass may be created,
1533 * and its members assigned values, via method <tt>::new</tt>:
1535 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1536 * joe # => #<struct Struct::Customer name="Joe Smith", address="123 Maple, Anytown NC", zip=12345>
1538 * The member values may be managed thus:
1540 * joe.name # => "Joe Smith"
1541 * joe.name = 'Joseph Smith'
1542 * joe.name # => "Joseph Smith"
1544 * And thus; note that member name may be expressed as either a string or a symbol:
1546 * joe[:name] # => "Joseph Smith"
1547 * joe[:name] = 'Joseph Smith, Jr.'
1548 * joe['name'] # => "Joseph Smith, Jr."
1554 * First, what's elsewhere. \Class \Struct:
1556 * - Inherits from {class Object}[Object.html#class-Object-label-What-27s+Here].
1557 * - Includes {module Enumerable}[Enumerable.html#module-Enumerable-label-What-27s+Here],
1558 * which provides dozens of additional methods.
1560 * Here, class \Struct provides methods that are useful for:
1562 * - {Creating a Struct Subclass}[#class-Struct-label-Methods+for+Creating+a+Struct+Subclass]
1563 * - {Querying}[#class-Struct-label-Methods+for+Querying]
1564 * - {Comparing}[#class-Struct-label-Methods+for+Comparing]
1565 * - {Fetching}[#class-Struct-label-Methods+for+Fetching]
1566 * - {Assigning}[#class-Struct-label-Methods+for+Assigning]
1567 * - {Iterating}[#class-Struct-label-Methods+for+Iterating]
1568 * - {Converting}[#class-Struct-label-Methods+for+Converting]
1570 * === Methods for Creating a Struct Subclass
1572 * ::new:: Returns a new subclass of \Struct.
1574 * === Methods for Querying
1576 * #hash:: Returns the integer hash code.
1577 * #length, #size:: Returns the number of members.
1579 * === Methods for Comparing
1581 * {#==}[#method-i-3D-3D]:: Returns whether a given object is equal to +self+,
1582 * using <tt>==</tt> to compare member values.
1583 * #eql?:: Returns whether a given object is equal to +self+,
1584 * using <tt>eql?</tt> to compare member values.
1586 * === Methods for Fetching
1588 * #[]:: Returns the value associated with a given member name.
1589 * #to_a, #values, #deconstruct:: Returns the member values in +self+ as an array.
1590 * #deconstruct_keys:: Returns a hash of the name/value pairs
1591 * for given member names.
1592 * #dig:: Returns the object in nested objects that is specified
1593 * by a given member name and additional arguments.
1594 * #members:: Returns an array of the member names.
1595 * #select, #filter:: Returns an array of member values from +self+,
1596 * as selected by the given block.
1597 * #values_at:: Returns an array containing values for given member names.
1599 * === Methods for Assigning
1601 * #[]=:: Assigns a given value to a given member name.
1603 * === Methods for Iterating
1605 * #each:: Calls a given block with each member name.
1606 * #each_pair:: Calls a given block with each member name/value pair.
1608 * === Methods for Converting
1610 * #inspect, #to_s:: Returns a string representation of +self+.
1611 * #to_h:: Returns a hash of the member name/value pairs in +self+.
1617 rb_cStruct
= rb_define_class("Struct", rb_cObject
);
1618 rb_include_module(rb_cStruct
, rb_mEnumerable
);
1620 rb_undef_alloc_func(rb_cStruct
);
1621 rb_define_singleton_method(rb_cStruct
, "new", rb_struct_s_def
, -1);
1622 #if 0 /* for RDoc */
1623 rb_define_singleton_method(rb_cStruct
, "keyword_init?", rb_struct_s_keyword_init_p
, 0);
1624 rb_define_singleton_method(rb_cStruct
, "members", rb_struct_s_members_m
, 0);
1627 rb_define_method(rb_cStruct
, "initialize", rb_struct_initialize_m
, -1);
1628 rb_define_method(rb_cStruct
, "initialize_copy", rb_struct_init_copy
, 1);
1630 rb_define_method(rb_cStruct
, "==", rb_struct_equal
, 1);
1631 rb_define_method(rb_cStruct
, "eql?", rb_struct_eql
, 1);
1632 rb_define_method(rb_cStruct
, "hash", rb_struct_hash
, 0);
1634 rb_define_method(rb_cStruct
, "inspect", rb_struct_inspect
, 0);
1635 rb_define_alias(rb_cStruct
, "to_s", "inspect");
1636 rb_define_method(rb_cStruct
, "to_a", rb_struct_to_a
, 0);
1637 rb_define_method(rb_cStruct
, "to_h", rb_struct_to_h
, 0);
1638 rb_define_method(rb_cStruct
, "values", rb_struct_to_a
, 0);
1639 rb_define_method(rb_cStruct
, "size", rb_struct_size
, 0);
1640 rb_define_method(rb_cStruct
, "length", rb_struct_size
, 0);
1642 rb_define_method(rb_cStruct
, "each", rb_struct_each
, 0);
1643 rb_define_method(rb_cStruct
, "each_pair", rb_struct_each_pair
, 0);
1644 rb_define_method(rb_cStruct
, "[]", rb_struct_aref
, 1);
1645 rb_define_method(rb_cStruct
, "[]=", rb_struct_aset
, 2);
1646 rb_define_method(rb_cStruct
, "select", rb_struct_select
, -1);
1647 rb_define_method(rb_cStruct
, "filter", rb_struct_select
, -1);
1648 rb_define_method(rb_cStruct
, "values_at", rb_struct_values_at
, -1);
1650 rb_define_method(rb_cStruct
, "members", rb_struct_members_m
, 0);
1651 rb_define_method(rb_cStruct
, "dig", rb_struct_dig
, -1);
1653 rb_define_method(rb_cStruct
, "deconstruct", rb_struct_to_a
, 0);
1654 rb_define_method(rb_cStruct
, "deconstruct_keys", rb_struct_deconstruct_keys
, 1);
1661 id_members
= rb_intern("__members__");
1662 id_back_members
= rb_intern("__members_back__");
1663 id_keyword_init
= rb_intern("__keyword_init__");