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 bool keyword_init
= false;
689 switch (rb_struct_s_keyword_init(klass
)) {
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
);
699 if (argc
> 1 || !RB_TYPE_P(argv
[0], T_HASH
)) {
702 keyword_init
= rb_keyword_given_p();
706 struct struct_hash_set_arg arg
;
707 rb_mem_clear((VALUE
*)RSTRUCT_CONST_PTR(self
), n
);
709 arg
.unknown_keywords
= Qnil
;
710 rb_hash_foreach(argv
[0], struct_hash_set_i
, (VALUE
)&arg
);
711 if (arg
.unknown_keywords
!= Qnil
) {
712 rb_raise(rb_eArgError
, "unknown keywords: %s",
713 RSTRING_PTR(rb_ary_join(arg
.unknown_keywords
, rb_str_new2(", "))));
718 rb_raise(rb_eArgError
, "struct size differs");
720 for (long i
=0; i
<argc
; i
++) {
721 RSTRUCT_SET(self
, i
, argv
[i
]);
724 rb_mem_clear((VALUE
*)RSTRUCT_CONST_PTR(self
)+argc
, n
-argc
);
731 rb_struct_initialize(VALUE self
, VALUE values
)
733 rb_struct_initialize_m(RARRAY_LENINT(values
), RARRAY_CONST_PTR(values
), self
);
739 struct_heap_alloc(VALUE st
, size_t len
)
741 VALUE
*ptr
= rb_transient_heap_alloc((VALUE
)st
, sizeof(VALUE
) * len
);
744 RSTRUCT_TRANSIENT_SET(st
);
748 RSTRUCT_TRANSIENT_UNSET(st
);
749 return ALLOC_N(VALUE
, len
);
753 #if USE_TRANSIENT_HEAP
755 rb_struct_transient_heap_evacuate(VALUE obj
, int promote
)
757 if (RSTRUCT_TRANSIENT_P(obj
)) {
758 const VALUE
*old_ptr
= rb_struct_const_heap_ptr(obj
);
760 long len
= RSTRUCT_LEN(obj
);
763 new_ptr
= ALLOC_N(VALUE
, len
);
764 FL_UNSET_RAW(obj
, RSTRUCT_TRANSIENT_FLAG
);
767 new_ptr
= struct_heap_alloc(obj
, len
);
769 MEMCPY(new_ptr
, old_ptr
, VALUE
, len
);
770 RSTRUCT(obj
)->as
.heap
.ptr
= new_ptr
;
776 struct_alloc(VALUE klass
)
779 NEWOBJ_OF(st
, struct RStruct
, klass
, T_STRUCT
| (RGENGC_WB_PROTECTED_STRUCT
? FL_WB_PROTECTED
: 0));
781 n
= num_members(klass
);
783 if (0 < n
&& n
<= RSTRUCT_EMBED_LEN_MAX
) {
784 RBASIC(st
)->flags
&= ~RSTRUCT_EMBED_LEN_MASK
;
785 RBASIC(st
)->flags
|= n
<< RSTRUCT_EMBED_LEN_SHIFT
;
786 rb_mem_clear((VALUE
*)st
->as
.ary
, n
);
789 st
->as
.heap
.ptr
= struct_heap_alloc((VALUE
)st
, n
);
790 rb_mem_clear((VALUE
*)st
->as
.heap
.ptr
, n
);
798 rb_struct_alloc(VALUE klass
, VALUE values
)
800 return rb_class_new_instance(RARRAY_LENINT(values
), RARRAY_CONST_PTR(values
), klass
);
804 rb_struct_new(VALUE klass
, ...)
806 VALUE tmpargs
[16], *mem
= tmpargs
;
810 size
= rb_long2int(num_members(klass
));
811 if (size
> numberof(tmpargs
)) {
812 tmpargs
[0] = rb_ary_tmp_new(size
);
813 mem
= RARRAY_PTR(tmpargs
[0]);
815 va_start(args
, klass
);
816 for (i
=0; i
<size
; i
++) {
817 mem
[i
] = va_arg(args
, VALUE
);
821 return rb_class_new_instance(size
, mem
, klass
);
825 struct_enum_size(VALUE s
, VALUE args
, VALUE eobj
)
827 return rb_struct_size(s
);
832 * each {|value| ... } -> self
835 * Calls the given block with the value of each member; returns +self+:
837 * Customer = Struct.new(:name, :address, :zip)
838 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
839 * joe.each {|value| p value }
844 * "123 Maple, Anytown NC"
847 * Returns an Enumerator if no block is given.
849 * Related: #each_pair.
853 rb_struct_each(VALUE s
)
857 RETURN_SIZED_ENUMERATOR(s
, 0, 0, struct_enum_size
);
858 for (i
=0; i
<RSTRUCT_LEN(s
); i
++) {
859 rb_yield(RSTRUCT_GET(s
, i
));
866 * each_pair {|(name, value)| ... } -> self
867 * each_pair -> enumerator
869 * Calls the given block with each member name/value pair; returns +self+:
871 * Customer = Struct.new(:name, :address, :zip) # => Customer
872 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
873 * joe.each_pair {|(name, value)| p "#{name} => #{value}" }
877 * "name => Joe Smith"
878 * "address => 123 Maple, Anytown NC"
881 * Returns an Enumerator if no block is given.
888 rb_struct_each_pair(VALUE s
)
893 RETURN_SIZED_ENUMERATOR(s
, 0, 0, struct_enum_size
);
894 members
= rb_struct_members(s
);
895 if (rb_block_pair_yield_optimizable()) {
896 for (i
=0; i
<RSTRUCT_LEN(s
); i
++) {
897 VALUE key
= rb_ary_entry(members
, i
);
898 VALUE value
= RSTRUCT_GET(s
, i
);
899 rb_yield_values(2, key
, value
);
903 for (i
=0; i
<RSTRUCT_LEN(s
); i
++) {
904 VALUE key
= rb_ary_entry(members
, i
);
905 VALUE value
= RSTRUCT_GET(s
, i
);
906 rb_yield(rb_assoc_new(key
, value
));
913 inspect_struct(VALUE s
, VALUE dummy
, int recur
)
915 VALUE cname
= rb_class_path(rb_obj_class(s
));
916 VALUE members
, str
= rb_str_new2("#<struct ");
918 char first
= RSTRING_PTR(cname
)[0];
920 if (recur
|| first
!= '#') {
921 rb_str_append(str
, cname
);
924 return rb_str_cat2(str
, ":...>");
927 members
= rb_struct_members(s
);
928 len
= RSTRUCT_LEN(s
);
930 for (i
=0; i
<len
; i
++) {
935 rb_str_cat2(str
, ", ");
937 else if (first
!= '#') {
938 rb_str_cat2(str
, " ");
940 slot
= RARRAY_AREF(members
, i
);
942 if (rb_is_local_id(id
) || rb_is_const_id(id
)) {
943 rb_str_append(str
, rb_id2str(id
));
946 rb_str_append(str
, rb_inspect(slot
));
948 rb_str_cat2(str
, "=");
949 rb_str_append(str
, rb_inspect(RSTRUCT_GET(s
, i
)));
951 rb_str_cat2(str
, ">");
960 * Returns a string representation of +self+:
962 * Customer = Struct.new(:name, :address, :zip) # => Customer
963 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
964 * joe.inspect # => "#<struct Customer name=\"Joe Smith\", address=\"123 Maple, Anytown NC\", zip=12345>"
966 * Struct#to_s is an alias for Struct#inspect.
971 rb_struct_inspect(VALUE s
)
973 return rb_exec_recursive(inspect_struct
, s
, 0);
980 * Returns the values in +self+ as an array:
982 * Customer = Struct.new(:name, :address, :zip)
983 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
984 * joe.to_a # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
986 * Struct#values and Struct#deconstruct are aliases for Struct#to_a.
992 rb_struct_to_a(VALUE s
)
994 return rb_ary_new4(RSTRUCT_LEN(s
), RSTRUCT_CONST_PTR(s
));
1000 * to_h {|name, value| ... } -> hash
1002 * Returns a hash containing the name and value for each member:
1004 * Customer = Struct.new(:name, :address, :zip)
1005 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1007 * h # => {:name=>"Joe Smith", :address=>"123 Maple, Anytown NC", :zip=>12345}
1009 * If a block is given, it is called with each name/value pair;
1010 * the block should return a 2-element array whose elements will become
1011 * a key/value pair in the returned hash:
1013 * h = joe.to_h{|name, value| [name.upcase, value.to_s.upcase]}
1014 * h # => {:NAME=>"JOE SMITH", :ADDRESS=>"123 MAPLE, ANYTOWN NC", :ZIP=>"12345"}
1016 * Raises ArgumentError if the block returns an inappropriate value.
1021 rb_struct_to_h(VALUE s
)
1023 VALUE h
= rb_hash_new_with_size(RSTRUCT_LEN(s
));
1024 VALUE members
= rb_struct_members(s
);
1026 int block_given
= rb_block_given_p();
1028 for (i
=0; i
<RSTRUCT_LEN(s
); i
++) {
1029 VALUE k
= rb_ary_entry(members
, i
), v
= RSTRUCT_GET(s
, i
);
1031 rb_hash_set_pair(h
, rb_yield_values(2, k
, v
));
1033 rb_hash_aset(h
, k
, v
);
1040 * deconstruct_keys(array_of_names) -> hash
1042 * Returns a hash of the name/value pairs for the given member names.
1044 * Customer = Struct.new(:name, :address, :zip)
1045 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1046 * h = joe.deconstruct_keys([:zip, :address])
1047 * h # => {:zip=>12345, :address=>"123 Maple, Anytown NC"}
1049 * Returns all names and values if +array_of_names+ is +nil+:
1051 * h = joe.deconstruct_keys(nil)
1052 * h # => {:name=>"Joseph Smith, Jr.", :address=>"123 Maple, Anytown NC", :zip=>12345}
1056 rb_struct_deconstruct_keys(VALUE s
, VALUE keys
)
1062 return rb_struct_to_h(s
);
1064 if (UNLIKELY(!RB_TYPE_P(keys
, T_ARRAY
))) {
1065 rb_raise(rb_eTypeError
,
1066 "wrong argument type %"PRIsVALUE
" (expected Array or nil)",
1067 rb_obj_class(keys
));
1070 if (RSTRUCT_LEN(s
) < RARRAY_LEN(keys
)) {
1071 return rb_hash_new_with_size(0);
1073 h
= rb_hash_new_with_size(RARRAY_LEN(keys
));
1074 for (i
=0; i
<RARRAY_LEN(keys
); i
++) {
1075 VALUE key
= RARRAY_AREF(keys
, i
);
1076 int i
= rb_struct_pos(s
, &key
);
1080 rb_hash_aset(h
, key
, RSTRUCT_GET(s
, i
));
1087 rb_struct_init_copy(VALUE copy
, VALUE s
)
1091 if (!OBJ_INIT_COPY(copy
, s
)) return copy
;
1092 if (RSTRUCT_LEN(copy
) != RSTRUCT_LEN(s
)) {
1093 rb_raise(rb_eTypeError
, "struct size mismatch");
1096 for (i
=0, len
=RSTRUCT_LEN(copy
); i
<len
; i
++) {
1097 RSTRUCT_SET(copy
, i
, RSTRUCT_GET(s
, i
));
1104 rb_struct_pos(VALUE s
, VALUE
*name
)
1109 if (SYMBOL_P(idx
)) {
1110 return struct_member_pos(s
, idx
);
1112 else if (RB_TYPE_P(idx
, T_STRING
)) {
1113 idx
= rb_check_symbol(name
);
1114 if (NIL_P(idx
)) return -1;
1115 return struct_member_pos(s
, idx
);
1120 len
= RSTRUCT_LEN(s
);
1123 *name
= LONG2FIX(i
);
1128 else if (len
<= i
) {
1129 *name
= LONG2FIX(i
);
1137 invalid_struct_pos(VALUE s
, VALUE idx
)
1139 if (FIXNUM_P(idx
)) {
1140 long i
= FIX2INT(idx
), len
= RSTRUCT_LEN(s
);
1142 rb_raise(rb_eIndexError
, "offset %ld too small for struct(size:%ld)",
1146 rb_raise(rb_eIndexError
, "offset %ld too large for struct(size:%ld)",
1151 rb_name_err_raise("no member '%1$s' in struct", s
, idx
);
1157 * struct[name] -> object
1158 * struct[n] -> object
1160 * Returns a value from +self+.
1162 * With symbol or string argument +name+ given, returns the value for the named member:
1164 * Customer = Struct.new(:name, :address, :zip)
1165 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1166 * joe[:zip] # => 12345
1168 * Raises NameError if +name+ is not the name of a member.
1170 * With integer argument +n+ given, returns <tt>self.values[n]</tt>
1171 * if +n+ is in range;
1172 * see {Array Indexes}[Array.html#class-Array-label-Array+Indexes]:
1175 * joe[-2] # => "123 Maple, Anytown NC"
1177 * Raises IndexError if +n+ is out of range.
1182 rb_struct_aref(VALUE s
, VALUE idx
)
1184 int i
= rb_struct_pos(s
, &idx
);
1185 if (i
< 0) invalid_struct_pos(s
, idx
);
1186 return RSTRUCT_GET(s
, i
);
1191 * struct[name] = value -> value
1192 * struct[n] = value -> value
1194 * Assigns a value to a member.
1196 * With symbol or string argument +name+ given, assigns the given +value+
1197 * to the named member; returns +value+:
1199 * Customer = Struct.new(:name, :address, :zip)
1200 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1201 * joe[:zip] = 54321 # => 54321
1202 * joe # => #<struct Customer name="Joe Smith", address="123 Maple, Anytown NC", zip=54321>
1204 * Raises NameError if +name+ is not the name of a member.
1206 * With integer argument +n+ given, assigns the given +value+
1207 * to the +n+-th member if +n+ is in range;
1208 * see {Array Indexes}[Array.html#class-Array-label-Array+Indexes]:
1210 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1211 * joe[2] = 54321 # => 54321
1212 * joe[-3] = 'Joseph Smith' # => "Joseph Smith"
1213 * joe # => #<struct Customer name="Joseph Smith", address="123 Maple, Anytown NC", zip=54321>
1215 * Raises IndexError if +n+ is out of range.
1220 rb_struct_aset(VALUE s
, VALUE idx
, VALUE val
)
1222 int i
= rb_struct_pos(s
, &idx
);
1223 if (i
< 0) invalid_struct_pos(s
, idx
);
1224 rb_struct_modify(s
);
1225 RSTRUCT_SET(s
, i
, val
);
1229 FUNC_MINIMIZED(VALUE
rb_struct_lookup(VALUE s
, VALUE idx
));
1230 NOINLINE(static VALUE
rb_struct_lookup_default(VALUE s
, VALUE idx
, VALUE notfound
));
1233 rb_struct_lookup(VALUE s
, VALUE idx
)
1235 return rb_struct_lookup_default(s
, idx
, Qnil
);
1239 rb_struct_lookup_default(VALUE s
, VALUE idx
, VALUE notfound
)
1241 int i
= rb_struct_pos(s
, &idx
);
1242 if (i
< 0) return notfound
;
1243 return RSTRUCT_GET(s
, i
);
1247 struct_entry(VALUE s
, long n
)
1249 return rb_struct_aref(s
, LONG2NUM(n
));
1254 * values_at(*integers) -> array
1255 * values_at(integer_range) -> array
1257 * Returns an array of values from +self+.
1259 * With integer arguments +integers+ given,
1260 * returns an array containing each value given by one of +integers+:
1262 * Customer = Struct.new(:name, :address, :zip)
1263 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1264 * joe.values_at(0, 2) # => ["Joe Smith", 12345]
1265 * joe.values_at(2, 0) # => [12345, "Joe Smith"]
1266 * joe.values_at(2, 1, 0) # => [12345, "123 Maple, Anytown NC", "Joe Smith"]
1267 * joe.values_at(0, -3) # => ["Joe Smith", "Joe Smith"]
1269 * Raises IndexError if any of +integers+ is out of range;
1270 * see {Array Indexes}[Array.html#class-Array-label-Array+Indexes].
1272 * With integer range argument +integer_range+ given,
1273 * returns an array containing each value given by the elements of the range;
1274 * fills with +nil+ values for range elements larger than the structure:
1276 * joe.values_at(0..2)
1277 * # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
1278 * joe.values_at(-3..-1)
1279 * # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
1280 * joe.values_at(1..4) # => ["123 Maple, Anytown NC", 12345, nil, nil]
1282 * Raises RangeError if any element of the range is negative and out of range;
1283 * see {Array Indexes}[Array.html#class-Array-label-Array+Indexes].
1288 rb_struct_values_at(int argc
, VALUE
*argv
, VALUE s
)
1290 return rb_get_values_at(s
, RSTRUCT_LEN(s
), argc
, argv
, struct_entry
);
1295 * select {|value| ... } -> array
1296 * select -> enumerator
1298 * With a block given, returns an array of values from +self+
1299 * for which the block returns a truthy value:
1301 * Customer = Struct.new(:name, :address, :zip)
1302 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1303 * a = joe.select {|value| value.is_a?(String) }
1304 * a # => ["Joe Smith", "123 Maple, Anytown NC"]
1305 * a = joe.select {|value| value.is_a?(Integer) }
1308 * With no block given, returns an Enumerator.
1310 * Struct#filter is an alias for Struct#select.
1314 rb_struct_select(int argc
, VALUE
*argv
, VALUE s
)
1319 rb_check_arity(argc
, 0, 0);
1320 RETURN_SIZED_ENUMERATOR(s
, 0, 0, struct_enum_size
);
1321 result
= rb_ary_new();
1322 for (i
= 0; i
< RSTRUCT_LEN(s
); i
++) {
1323 if (RTEST(rb_yield(RSTRUCT_GET(s
, i
)))) {
1324 rb_ary_push(result
, RSTRUCT_GET(s
, i
));
1332 recursive_equal(VALUE s
, VALUE s2
, int recur
)
1336 if (recur
) return Qtrue
; /* Subtle! */
1337 len
= RSTRUCT_LEN(s
);
1338 for (i
=0; i
<len
; i
++) {
1339 if (!rb_equal(RSTRUCT_GET(s
, i
), RSTRUCT_GET(s2
, i
))) return Qfalse
;
1347 * self == other -> true or false
1349 * Returns +true+ if and only if the following are true; otherwise returns +false+:
1351 * - <tt>other.class == self.class</tt>.
1352 * - For each member name +name+, <tt>other.name == self.name</tt>.
1356 * Customer = Struct.new(:name, :address, :zip)
1357 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1358 * joe_jr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1359 * joe_jr == joe # => true
1360 * joe_jr[:name] = 'Joe Smith, Jr.'
1361 * # => "Joe Smith, Jr."
1362 * joe_jr == joe # => false
1366 rb_struct_equal(VALUE s
, VALUE s2
)
1368 if (s
== s2
) return Qtrue
;
1369 if (!RB_TYPE_P(s2
, T_STRUCT
)) return Qfalse
;
1370 if (rb_obj_class(s
) != rb_obj_class(s2
)) return Qfalse
;
1371 if (RSTRUCT_LEN(s
) != RSTRUCT_LEN(s2
)) {
1372 rb_bug("inconsistent struct"); /* should never happen */
1375 return rb_exec_recursive_paired(recursive_equal
, s
, s2
, s2
);
1382 * Returns the integer hash value for +self+.
1384 * Two structs of the same class and with the same content
1385 * will have the same hash code (and will compare using Struct#eql?):
1387 * Customer = Struct.new(:name, :address, :zip)
1388 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1389 * joe_jr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1390 * joe.hash == joe_jr.hash # => true
1391 * joe_jr[:name] = 'Joe Smith, Jr.'
1392 * joe.hash == joe_jr.hash # => false
1394 * Related: Object#hash.
1398 rb_struct_hash(VALUE s
)
1404 h
= rb_hash_start(rb_hash(rb_obj_class(s
)));
1405 len
= RSTRUCT_LEN(s
);
1406 for (i
= 0; i
< len
; i
++) {
1407 n
= rb_hash(RSTRUCT_GET(s
, i
));
1408 h
= rb_hash_uint(h
, NUM2LONG(n
));
1415 recursive_eql(VALUE s
, VALUE s2
, int recur
)
1419 if (recur
) return Qtrue
; /* Subtle! */
1420 len
= RSTRUCT_LEN(s
);
1421 for (i
=0; i
<len
; i
++) {
1422 if (!rb_eql(RSTRUCT_GET(s
, i
), RSTRUCT_GET(s2
, i
))) return Qfalse
;
1429 * eql?(other) -> true or false
1431 * Returns +true+ if and only if the following are true; otherwise returns +false+:
1433 * - <tt>other.class == self.class</tt>.
1434 * - For each member name +name+, <tt>other.name.eql?(self.name)</tt>.
1436 * Customer = Struct.new(:name, :address, :zip)
1437 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1438 * joe_jr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1439 * joe_jr.eql?(joe) # => true
1440 * joe_jr[:name] = 'Joe Smith, Jr.'
1441 * joe_jr.eql?(joe) # => false
1443 * Related: Object#==.
1447 rb_struct_eql(VALUE s
, VALUE s2
)
1449 if (s
== s2
) return Qtrue
;
1450 if (!RB_TYPE_P(s2
, T_STRUCT
)) return Qfalse
;
1451 if (rb_obj_class(s
) != rb_obj_class(s2
)) return Qfalse
;
1452 if (RSTRUCT_LEN(s
) != RSTRUCT_LEN(s2
)) {
1453 rb_bug("inconsistent struct"); /* should never happen */
1456 return rb_exec_recursive_paired(recursive_eql
, s
, s2
, s2
);
1463 * Returns the number of members.
1465 * Customer = Struct.new(:name, :address, :zip)
1466 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1469 * Struct#length is an alias for Struct#size.
1473 rb_struct_size(VALUE s
)
1475 return LONG2FIX(RSTRUCT_LEN(s
));
1480 * dig(name, *identifiers) -> object
1481 * dig(n, *identifiers) -> object
1483 * Finds and returns an object among nested objects.
1484 * The nested objects may be instances of various classes.
1485 * See {Dig Methods}[rdoc-ref:dig_methods.rdoc].
1488 * Given symbol or string argument +name+,
1489 * returns the object that is specified by +name+ and +identifiers+:
1491 * Foo = Struct.new(:a)
1492 * f = Foo.new(Foo.new({b: [1, 2, 3]}))
1493 * f.dig(:a) # => #<struct Foo a={:b=>[1, 2, 3]}>
1494 * f.dig(:a, :a) # => {:b=>[1, 2, 3]}
1495 * f.dig(:a, :a, :b) # => [1, 2, 3]
1496 * f.dig(:a, :a, :b, 0) # => 1
1497 * f.dig(:b, 0) # => nil
1499 * Given integer argument +n+,
1500 * returns the object that is specified by +n+ and +identifiers+:
1502 * f.dig(0) # => #<struct Foo a={:b=>[1, 2, 3]}>
1503 * f.dig(0, 0) # => {:b=>[1, 2, 3]}
1504 * f.dig(0, 0, :b) # => [1, 2, 3]
1505 * f.dig(0, 0, :b, 0) # => 1
1506 * f.dig(:b, 0) # => nil
1511 rb_struct_dig(int argc
, VALUE
*argv
, VALUE self
)
1513 rb_check_arity(argc
, 1, UNLIMITED_ARGUMENTS
);
1514 self
= rb_struct_lookup(self
, *argv
);
1515 if (!--argc
) return self
;
1517 return rb_obj_dig(argc
, argv
, self
, Qnil
);
1521 * Document-class: Struct
1523 * \Class \Struct provides a convenient way to create a simple class
1524 * that can store and fetch values.
1526 * This example creates a subclass of +Struct+, <tt>Struct::Customer</tt>;
1527 * the first argument, a string, is the name of the subclass;
1528 * the other arguments, symbols, determine the _members_ of the new subclass.
1530 * Customer = Struct.new('Customer', :name, :address, :zip)
1531 * Customer.name # => "Struct::Customer"
1532 * Customer.class # => Class
1533 * Customer.superclass # => Struct
1535 * Corresponding to each member are two methods, a writer and a reader,
1536 * that store and fetch values:
1538 * methods = Customer.instance_methods false
1539 * methods # => [:zip, :address=, :zip=, :address, :name, :name=]
1541 * An instance of the subclass may be created,
1542 * and its members assigned values, via method <tt>::new</tt>:
1544 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
1545 * joe # => #<struct Struct::Customer name="Joe Smith", address="123 Maple, Anytown NC", zip=12345>
1547 * The member values may be managed thus:
1549 * joe.name # => "Joe Smith"
1550 * joe.name = 'Joseph Smith'
1551 * joe.name # => "Joseph Smith"
1553 * And thus; note that member name may be expressed as either a string or a symbol:
1555 * joe[:name] # => "Joseph Smith"
1556 * joe[:name] = 'Joseph Smith, Jr.'
1557 * joe['name'] # => "Joseph Smith, Jr."
1563 * First, what's elsewhere. \Class \Struct:
1565 * - Inherits from {class Object}[Object.html#class-Object-label-What-27s+Here].
1566 * - Includes {module Enumerable}[Enumerable.html#module-Enumerable-label-What-27s+Here],
1567 * which provides dozens of additional methods.
1569 * Here, class \Struct provides methods that are useful for:
1571 * - {Creating a Struct Subclass}[#class-Struct-label-Methods+for+Creating+a+Struct+Subclass]
1572 * - {Querying}[#class-Struct-label-Methods+for+Querying]
1573 * - {Comparing}[#class-Struct-label-Methods+for+Comparing]
1574 * - {Fetching}[#class-Struct-label-Methods+for+Fetching]
1575 * - {Assigning}[#class-Struct-label-Methods+for+Assigning]
1576 * - {Iterating}[#class-Struct-label-Methods+for+Iterating]
1577 * - {Converting}[#class-Struct-label-Methods+for+Converting]
1579 * === Methods for Creating a Struct Subclass
1581 * ::new:: Returns a new subclass of \Struct.
1583 * === Methods for Querying
1585 * #hash:: Returns the integer hash code.
1586 * #length, #size:: Returns the number of members.
1588 * === Methods for Comparing
1590 * {#==}[#method-i-3D-3D]:: Returns whether a given object is equal to +self+,
1591 * using <tt>==</tt> to compare member values.
1592 * #eql?:: Returns whether a given object is equal to +self+,
1593 * using <tt>eql?</tt> to compare member values.
1595 * === Methods for Fetching
1597 * #[]:: Returns the value associated with a given member name.
1598 * #to_a, #values, #deconstruct:: Returns the member values in +self+ as an array.
1599 * #deconstruct_keys:: Returns a hash of the name/value pairs
1600 * for given member names.
1601 * #dig:: Returns the object in nested objects that is specified
1602 * by a given member name and additional arguments.
1603 * #members:: Returns an array of the member names.
1604 * #select, #filter:: Returns an array of member values from +self+,
1605 * as selected by the given block.
1606 * #values_at:: Returns an array containing values for given member names.
1608 * === Methods for Assigning
1610 * #[]=:: Assigns a given value to a given member name.
1612 * === Methods for Iterating
1614 * #each:: Calls a given block with each member name.
1615 * #each_pair:: Calls a given block with each member name/value pair.
1617 * === Methods for Converting
1619 * #inspect, #to_s:: Returns a string representation of +self+.
1620 * #to_h:: Returns a hash of the member name/value pairs in +self+.
1626 rb_cStruct
= rb_define_class("Struct", rb_cObject
);
1627 rb_include_module(rb_cStruct
, rb_mEnumerable
);
1629 rb_undef_alloc_func(rb_cStruct
);
1630 rb_define_singleton_method(rb_cStruct
, "new", rb_struct_s_def
, -1);
1631 #if 0 /* for RDoc */
1632 rb_define_singleton_method(rb_cStruct
, "keyword_init?", rb_struct_s_keyword_init_p
, 0);
1633 rb_define_singleton_method(rb_cStruct
, "members", rb_struct_s_members_m
, 0);
1636 rb_define_method(rb_cStruct
, "initialize", rb_struct_initialize_m
, -1);
1637 rb_define_method(rb_cStruct
, "initialize_copy", rb_struct_init_copy
, 1);
1639 rb_define_method(rb_cStruct
, "==", rb_struct_equal
, 1);
1640 rb_define_method(rb_cStruct
, "eql?", rb_struct_eql
, 1);
1641 rb_define_method(rb_cStruct
, "hash", rb_struct_hash
, 0);
1643 rb_define_method(rb_cStruct
, "inspect", rb_struct_inspect
, 0);
1644 rb_define_alias(rb_cStruct
, "to_s", "inspect");
1645 rb_define_method(rb_cStruct
, "to_a", rb_struct_to_a
, 0);
1646 rb_define_method(rb_cStruct
, "to_h", rb_struct_to_h
, 0);
1647 rb_define_method(rb_cStruct
, "values", rb_struct_to_a
, 0);
1648 rb_define_method(rb_cStruct
, "size", rb_struct_size
, 0);
1649 rb_define_method(rb_cStruct
, "length", rb_struct_size
, 0);
1651 rb_define_method(rb_cStruct
, "each", rb_struct_each
, 0);
1652 rb_define_method(rb_cStruct
, "each_pair", rb_struct_each_pair
, 0);
1653 rb_define_method(rb_cStruct
, "[]", rb_struct_aref
, 1);
1654 rb_define_method(rb_cStruct
, "[]=", rb_struct_aset
, 2);
1655 rb_define_method(rb_cStruct
, "select", rb_struct_select
, -1);
1656 rb_define_method(rb_cStruct
, "filter", rb_struct_select
, -1);
1657 rb_define_method(rb_cStruct
, "values_at", rb_struct_values_at
, -1);
1659 rb_define_method(rb_cStruct
, "members", rb_struct_members_m
, 0);
1660 rb_define_method(rb_cStruct
, "dig", rb_struct_dig
, -1);
1662 rb_define_method(rb_cStruct
, "deconstruct", rb_struct_to_a
, 0);
1663 rb_define_method(rb_cStruct
, "deconstruct_keys", rb_struct_deconstruct_keys
, 1);
1670 id_members
= rb_intern("__members__");
1671 id_back_members
= rb_intern("__members_back__");
1672 id_keyword_init
= rb_intern("__keyword_init__");