1 /**********************************************************************
6 created at: Tue Mar 22 18:44:30 JST 1995
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/ruby.h"
16 static VALUE
struct_alloc(VALUE
);
19 rb_struct_iv_get(VALUE c
, const char *name
)
25 if (rb_ivar_defined(c
, id
))
26 return rb_ivar_get(c
, id
);
28 if (c
== 0 || c
== rb_cStruct
)
34 rb_struct_s_members(VALUE klass
)
36 VALUE members
= rb_struct_iv_get(klass
, "__members__");
39 rb_raise(rb_eTypeError
, "uninitialized struct");
41 if (TYPE(members
) != T_ARRAY
) {
42 rb_raise(rb_eTypeError
, "corrupted struct");
48 rb_struct_members(VALUE s
)
50 VALUE members
= rb_struct_s_members(rb_obj_class(s
));
52 if (RSTRUCT_LEN(s
) != RARRAY_LEN(members
)) {
53 rb_raise(rb_eTypeError
, "struct size differs (%ld required %ld given)",
54 RARRAY_LEN(members
), RSTRUCT_LEN(s
));
60 rb_struct_s_members_m(VALUE klass
)
65 members
= rb_struct_s_members(klass
);
66 ary
= rb_ary_new2(RARRAY_LEN(members
));
67 p
= RARRAY_PTR(members
); pend
= p
+ RARRAY_LEN(members
);
78 * struct.members => array
80 * Returns an array of strings representing the names of the instance
83 * Customer = Struct.new(:name, :address, :zip)
84 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
85 * joe.members #=> [:name, :address, :zip]
89 rb_struct_members_m(VALUE obj
)
91 return rb_struct_s_members_m(rb_obj_class(obj
));
95 rb_struct_getmember(VALUE obj
, ID id
)
100 members
= rb_struct_members(obj
);
102 for (i
=0; i
<RARRAY_LEN(members
); i
++) {
103 if (RARRAY_PTR(members
)[i
] == slot
) {
104 return RSTRUCT_PTR(obj
)[i
];
107 rb_name_error(id
, "%s is not struct member", rb_id2name(id
));
108 return Qnil
; /* not reached */
112 rb_struct_ref(VALUE obj
)
114 return rb_struct_getmember(obj
, rb_frame_this_func());
117 static VALUE
rb_struct_ref0(VALUE obj
) {return RSTRUCT_PTR(obj
)[0];}
118 static VALUE
rb_struct_ref1(VALUE obj
) {return RSTRUCT_PTR(obj
)[1];}
119 static VALUE
rb_struct_ref2(VALUE obj
) {return RSTRUCT_PTR(obj
)[2];}
120 static VALUE
rb_struct_ref3(VALUE obj
) {return RSTRUCT_PTR(obj
)[3];}
121 static VALUE
rb_struct_ref4(VALUE obj
) {return RSTRUCT_PTR(obj
)[4];}
122 static VALUE
rb_struct_ref5(VALUE obj
) {return RSTRUCT_PTR(obj
)[5];}
123 static VALUE
rb_struct_ref6(VALUE obj
) {return RSTRUCT_PTR(obj
)[6];}
124 static VALUE
rb_struct_ref7(VALUE obj
) {return RSTRUCT_PTR(obj
)[7];}
125 static VALUE
rb_struct_ref8(VALUE obj
) {return RSTRUCT_PTR(obj
)[8];}
126 static VALUE
rb_struct_ref9(VALUE obj
) {return RSTRUCT_PTR(obj
)[9];}
128 #define N_REF_FUNC (sizeof(ref_func) / sizeof(ref_func[0]))
130 static VALUE (*const ref_func
[])(VALUE
) = {
144 rb_struct_modify(VALUE s
)
146 if (OBJ_FROZEN(s
)) rb_error_frozen("Struct");
147 if (!OBJ_TAINTED(s
) && rb_safe_level() >= 4)
148 rb_raise(rb_eSecurityError
, "Insecure: can't modify Struct");
152 rb_struct_set(VALUE obj
, VALUE val
)
157 members
= rb_struct_members(obj
);
158 rb_struct_modify(obj
);
159 for (i
=0; i
<RARRAY_LEN(members
); i
++) {
160 slot
= RARRAY_PTR(members
)[i
];
161 if (rb_id_attrset(SYM2ID(slot
)) == rb_frame_this_func()) {
162 return RSTRUCT_PTR(obj
)[i
] = val
;
165 rb_name_error(rb_frame_this_func(), "`%s' is not a struct member",
166 rb_id2name(rb_frame_this_func()));
167 return Qnil
; /* not reached */
171 make_struct(VALUE name
, VALUE members
, VALUE klass
)
179 nstr
= rb_class_new(klass
);
180 rb_make_metaclass(nstr
, RBASIC(klass
)->klass
);
181 rb_class_inherited(klass
, nstr
);
184 /* old style: should we warn? */
185 name
= rb_str_to_str(name
);
187 if (!rb_is_const_id(id
)) {
188 rb_name_error(id
, "identifier %s needs to be constant", StringValuePtr(name
));
190 if (rb_const_defined_at(klass
, id
)) {
191 rb_warn("redefining constant Struct::%s", StringValuePtr(name
));
192 rb_mod_remove_const(klass
, ID2SYM(id
));
194 nstr
= rb_define_class_under(klass
, rb_id2name(id
), klass
);
196 rb_iv_set(nstr
, "__members__", members
);
198 rb_define_alloc_func(nstr
, struct_alloc
);
199 rb_define_singleton_method(nstr
, "new", rb_class_new_instance
, -1);
200 rb_define_singleton_method(nstr
, "[]", rb_class_new_instance
, -1);
201 rb_define_singleton_method(nstr
, "members", rb_struct_s_members_m
, 0);
202 for (i
=0; i
< RARRAY_LEN(members
); i
++) {
203 ID id
= SYM2ID(RARRAY_PTR(members
)[i
]);
204 if (rb_is_local_id(id
) || rb_is_const_id(id
)) {
205 if (i
< N_REF_FUNC
) {
206 rb_define_method_id(nstr
, id
, ref_func
[i
], 0);
209 rb_define_method_id(nstr
, id
, rb_struct_ref
, 0);
211 rb_define_method_id(nstr
, rb_id_attrset(id
), rb_struct_set
, 1);
219 rb_struct_alloc_noinit(VALUE klass
)
221 return struct_alloc(klass
);
225 rb_struct_define_without_accessor(const char *class_name
, VALUE super
, rb_alloc_func_t alloc
, ...)
233 members
= rb_ary_new2(0);
236 while ((name
= va_arg(ar
, char*)) != NULL
) {
237 rb_ary_push(members
, ID2SYM(rb_intern(name
)));
243 klass
= rb_define_class(class_name
, super
);
246 klass
= rb_class_new(super
);
247 rb_make_metaclass(klass
, RBASIC(super
)->klass
);
248 rb_class_inherited(super
, klass
);
251 rb_iv_set(klass
, "__members__", members
);
254 rb_define_alloc_func(klass
, alloc
);
256 rb_define_alloc_func(klass
, struct_alloc
);
262 rb_struct_define(const char *name
, ...)
268 if (!name
) nm
= Qnil
;
269 else nm
= rb_str_new2(name
);
273 while ((mem
= va_arg(ar
, char*)) != 0) {
274 ID slot
= rb_intern(mem
);
275 rb_ary_push(ary
, ID2SYM(slot
));
279 return make_struct(nm
, ary
, rb_cStruct
);
284 * Struct.new( [aString] [, aSym]+> ) => StructClass
285 * StructClass.new(arg, ...) => obj
286 * StructClass[arg, ...] => obj
288 * Creates a new class, named by <i>aString</i>, containing accessor
289 * methods for the given symbols. If the name <i>aString</i> is
290 * omitted, an anonymous structure class will be created. Otherwise,
291 * the name of this struct will appear as a constant in class
292 * <code>Struct</code>, so it must be unique for all
293 * <code>Struct</code>s in the system and should start with a capital
294 * letter. Assigning a structure class to a constant effectively gives
295 * the class the name of the constant.
297 * <code>Struct::new</code> returns a new <code>Class</code> object,
298 * which can then be used to create specific instances of the new
299 * structure. The number of actual parameters must be
300 * less than or equal to the number of attributes defined for this
301 * class; unset parameters default to \nil{}. Passing too many
302 * parameters will raise an \E{ArgumentError}.
304 * The remaining methods listed in this section (class and instance)
305 * are defined for this generated class.
307 * # Create a structure with a name in Struct
308 * Struct.new("Customer", :name, :address) #=> Struct::Customer
309 * Struct::Customer.new("Dave", "123 Main") #=> #<struct Struct::Customer name="Dave", address="123 Main">
311 * # Create a structure named by its constant
312 * Customer = Struct.new(:name, :address) #=> Customer
313 * Customer.new("Dave", "123 Main") #=> #<struct Customer name="Dave", address="123 Main">
317 rb_struct_s_def(int argc
, VALUE
*argv
, VALUE klass
)
324 rb_scan_args(argc
, argv
, "1*", &name
, &rest
);
325 if (!NIL_P(name
) && SYMBOL_P(name
)) {
326 rb_ary_unshift(rest
, name
);
329 for (i
=0; i
<RARRAY_LEN(rest
); i
++) {
330 id
= rb_to_id(RARRAY_PTR(rest
)[i
]);
331 RARRAY_PTR(rest
)[i
] = ID2SYM(id
);
333 st
= make_struct(name
, rest
, klass
);
334 if (rb_block_given_p()) {
335 rb_mod_module_eval(0, 0, st
);
342 num_members(VALUE klass
)
345 members
= rb_struct_iv_get(klass
, "__members__");
346 if (TYPE(members
) != T_ARRAY
) {
347 rb_raise(rb_eTypeError
, "broken members");
349 return RARRAY_LEN(members
);
356 rb_struct_initialize(VALUE self
, VALUE values
)
358 VALUE klass
= rb_obj_class(self
);
361 rb_struct_modify(self
);
362 n
= num_members(klass
);
363 if (n
< RARRAY_LEN(values
)) {
364 rb_raise(rb_eArgError
, "struct size differs");
366 MEMCPY(RSTRUCT_PTR(self
), RARRAY_PTR(values
), VALUE
, RARRAY_LEN(values
));
367 if (n
> RARRAY_LEN(values
)) {
368 rb_mem_clear(RSTRUCT_PTR(self
)+RARRAY_LEN(values
),
369 n
-RARRAY_LEN(values
));
375 struct_alloc(VALUE klass
)
378 NEWOBJ(st
, struct RStruct
);
379 OBJSETUP(st
, klass
, T_STRUCT
);
381 n
= num_members(klass
);
383 if (0 < n
&& n
<= RSTRUCT_EMBED_LEN_MAX
) {
384 RBASIC(st
)->flags
&= ~RSTRUCT_EMBED_LEN_MASK
;
385 RBASIC(st
)->flags
|= n
<< RSTRUCT_EMBED_LEN_SHIFT
;
386 rb_mem_clear(st
->as
.ary
, n
);
389 st
->as
.heap
.ptr
= ALLOC_N(VALUE
, n
);
390 rb_mem_clear(st
->as
.heap
.ptr
, n
);
398 rb_struct_alloc(VALUE klass
, VALUE values
)
400 return rb_class_new_instance(RARRAY_LEN(values
), RARRAY_PTR(values
), klass
);
404 rb_struct_new(VALUE klass
, ...)
410 size
= num_members(klass
);
411 mem
= ALLOCA_N(VALUE
, size
);
412 va_start(args
, klass
);
413 for (i
=0; i
<size
; i
++) {
414 mem
[i
] = va_arg(args
, VALUE
);
418 return rb_class_new_instance(size
, mem
, klass
);
423 * struct.each {|obj| block } => struct
425 * Calls <i>block</i> once for each instance variable, passing the
426 * value as a parameter.
428 * Customer = Struct.new(:name, :address, :zip)
429 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
430 * joe.each {|x| puts(x) }
435 * 123 Maple, Anytown NC
440 rb_struct_each(VALUE s
)
444 RETURN_ENUMERATOR(s
, 0, 0);
445 for (i
=0; i
<RSTRUCT_LEN(s
); i
++) {
446 rb_yield(RSTRUCT_PTR(s
)[i
]);
453 * struct.each_pair {|sym, obj| block } => struct
455 * Calls <i>block</i> once for each instance variable, passing the name
456 * (as a symbol) and the value as parameters.
458 * Customer = Struct.new(:name, :address, :zip)
459 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
460 * joe.each_pair {|name, value| puts("#{name} => #{value}") }
465 * address => 123 Maple, Anytown NC
470 rb_struct_each_pair(VALUE s
)
475 RETURN_ENUMERATOR(s
, 0, 0);
476 members
= rb_struct_members(s
);
477 for (i
=0; i
<RSTRUCT_LEN(s
); i
++) {
478 rb_yield_values(2, rb_ary_entry(members
, i
), RSTRUCT_PTR(s
)[i
]);
484 inspect_struct(VALUE s
, VALUE dummy
, int recur
)
486 const char *cname
= rb_class2name(rb_obj_class(s
));
491 return rb_sprintf("#<struct %s:...>", cname
);
494 members
= rb_struct_members(s
);
495 if (cname
[0] == '#') {
496 str
= rb_str_new2("#<struct ");
499 str
= rb_sprintf("#<struct %s ", cname
);
501 for (i
=0; i
<RSTRUCT_LEN(s
); i
++) {
506 rb_str_cat2(str
, ", ");
508 slot
= RARRAY_PTR(members
)[i
];
510 if (rb_is_local_id(id
) || rb_is_const_id(id
)) {
511 rb_str_append(str
, rb_id2str(id
));
514 rb_str_append(str
, rb_inspect(slot
));
516 rb_str_cat2(str
, "=");
517 rb_str_append(str
, rb_inspect(RSTRUCT_PTR(s
)[i
]));
519 rb_str_cat2(str
, ">");
527 * struct.to_s => string
528 * struct.inspect => string
530 * Describe the contents of this struct in a string.
534 rb_struct_inspect(VALUE s
)
536 return rb_exec_recursive(inspect_struct
, s
, 0);
541 * struct.to_a => array
542 * struct.values => array
544 * Returns the values for this instance as an array.
546 * Customer = Struct.new(:name, :address, :zip)
547 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
548 * joe.to_a[1] #=> "123 Maple, Anytown NC"
552 rb_struct_to_a(VALUE s
)
554 return rb_ary_new4(RSTRUCT_LEN(s
), RSTRUCT_PTR(s
));
559 rb_struct_init_copy(VALUE copy
, VALUE s
)
561 if (copy
== s
) return copy
;
562 rb_check_frozen(copy
);
563 if (!rb_obj_is_instance_of(s
, rb_obj_class(copy
))) {
564 rb_raise(rb_eTypeError
, "wrong argument class");
566 if (RSTRUCT_LEN(copy
) != RSTRUCT_LEN(s
)) {
567 rb_raise(rb_eTypeError
, "struct size mismatch");
569 MEMCPY(RSTRUCT_PTR(copy
), RSTRUCT_PTR(s
), VALUE
, RSTRUCT_LEN(copy
));
575 rb_struct_aref_id(VALUE s
, ID id
)
580 members
= rb_struct_members(s
);
581 len
= RARRAY_LEN(members
);
582 for (i
=0; i
<len
; i
++) {
583 if (SYM2ID(RARRAY_PTR(members
)[i
]) == id
) {
584 return RSTRUCT_PTR(s
)[i
];
587 rb_name_error(id
, "no member '%s' in struct", rb_id2name(id
));
588 return Qnil
; /* not reached */
593 * struct[symbol] => anObject
594 * struct[fixnum] => anObject
596 * Attribute Reference---Returns the value of the instance variable
597 * named by <i>symbol</i>, or indexed (0..length-1) by
598 * <i>fixnum</i>. Will raise <code>NameError</code> if the named
599 * variable does not exist, or <code>IndexError</code> if the index is
602 * Customer = Struct.new(:name, :address, :zip)
603 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
605 * joe["name"] #=> "Joe Smith"
606 * joe[:name] #=> "Joe Smith"
607 * joe[0] #=> "Joe Smith"
611 rb_struct_aref(VALUE s
, VALUE idx
)
615 if (TYPE(idx
) == T_STRING
|| TYPE(idx
) == T_SYMBOL
) {
616 return rb_struct_aref_id(s
, rb_to_id(idx
));
620 if (i
< 0) i
= RSTRUCT_LEN(s
) + i
;
622 rb_raise(rb_eIndexError
, "offset %ld too small for struct(size:%ld)",
624 if (RSTRUCT_LEN(s
) <= i
)
625 rb_raise(rb_eIndexError
, "offset %ld too large for struct(size:%ld)",
627 return RSTRUCT_PTR(s
)[i
];
631 rb_struct_aset_id(VALUE s
, ID id
, VALUE val
)
636 members
= rb_struct_members(s
);
638 len
= RARRAY_LEN(members
);
639 if (RSTRUCT_LEN(s
) != RARRAY_LEN(members
)) {
640 rb_raise(rb_eTypeError
, "struct size differs (%ld required %ld given)",
641 RARRAY_LEN(members
), RSTRUCT_LEN(s
));
643 for (i
=0; i
<len
; i
++) {
644 if (SYM2ID(RARRAY_PTR(members
)[i
]) == id
) {
645 RSTRUCT_PTR(s
)[i
] = val
;
649 rb_name_error(id
, "no member '%s' in struct", rb_id2name(id
));
654 * struct[symbol] = obj => obj
655 * struct[fixnum] = obj => obj
657 * Attribute Assignment---Assigns to the instance variable named by
658 * <i>symbol</i> or <i>fixnum</i> the value <i>obj</i> and
659 * returns it. Will raise a <code>NameError</code> if the named
660 * variable does not exist, or an <code>IndexError</code> if the index
663 * Customer = Struct.new(:name, :address, :zip)
664 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
666 * joe["name"] = "Luke"
667 * joe[:zip] = "90210"
669 * joe.name #=> "Luke"
670 * joe.zip #=> "90210"
674 rb_struct_aset(VALUE s
, VALUE idx
, VALUE val
)
678 if (TYPE(idx
) == T_STRING
|| TYPE(idx
) == T_SYMBOL
) {
679 return rb_struct_aset_id(s
, rb_to_id(idx
), val
);
683 if (i
< 0) i
= RSTRUCT_LEN(s
) + i
;
685 rb_raise(rb_eIndexError
, "offset %ld too small for struct(size:%ld)",
688 if (RSTRUCT_LEN(s
) <= i
) {
689 rb_raise(rb_eIndexError
, "offset %ld too large for struct(size:%ld)",
693 return RSTRUCT_PTR(s
)[i
] = val
;
697 struct_entry(VALUE s
, long n
)
699 return rb_struct_aref(s
, LONG2NUM(n
));
704 * struct.values_at(selector,... ) => an_array
706 * Returns an array containing the elements in
707 * _self_ corresponding to the given selector(s). The selectors
708 * may be either integer indices or ranges.
709 * See also </code>.select<code>.
711 * a = %w{ a b c d e f }
712 * a.values_at(1, 3, 5)
713 * a.values_at(1, 3, 5, 7)
714 * a.values_at(-1, -3, -5, -7)
715 * a.values_at(1..3, 2...5)
719 rb_struct_values_at(int argc
, VALUE
*argv
, VALUE s
)
721 return rb_get_values_at(s
, RSTRUCT_LEN(s
), argc
, argv
, struct_entry
);
726 * struct.select {|i| block } => array
728 * Invokes the block passing in successive elements from
729 * <i>struct</i>, returning an array containing those elements
730 * for which the block returns a true value (equivalent to
731 * <code>Enumerable#select</code>).
733 * Lots = Struct.new(:a, :b, :c, :d, :e, :f)
734 * l = Lots.new(11, 22, 33, 44, 55, 66)
735 * l.select {|v| (v % 2).zero? } #=> [22, 44, 66]
739 rb_struct_select(int argc
, VALUE
*argv
, VALUE s
)
745 rb_raise(rb_eArgError
, "wrong number of arguments (%d for 0)", argc
);
747 result
= rb_ary_new();
748 for (i
= 0; i
< RSTRUCT_LEN(s
); i
++) {
749 if (RTEST(rb_yield(RSTRUCT_PTR(s
)[i
]))) {
750 rb_ary_push(result
, RSTRUCT_PTR(s
)[i
]);
759 * struct == other_struct => true or false
761 * Equality---Returns <code>true</code> if <i>other_struct</i> is
762 * equal to this one: they must be of the same class as generated by
763 * <code>Struct::new</code>, and the values of all instance variables
764 * must be equal (according to <code>Object#==</code>).
766 * Customer = Struct.new(:name, :address, :zip)
767 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
768 * joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
769 * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345)
770 * joe == joejr #=> true
771 * joe == jane #=> false
775 rb_struct_equal(VALUE s
, VALUE s2
)
779 if (s
== s2
) return Qtrue
;
780 if (TYPE(s2
) != T_STRUCT
) return Qfalse
;
781 if (rb_obj_class(s
) != rb_obj_class(s2
)) return Qfalse
;
782 if (RSTRUCT_LEN(s
) != RSTRUCT_LEN(s2
)) {
783 rb_bug("inconsistent struct"); /* should never happen */
786 for (i
=0; i
<RSTRUCT_LEN(s
); i
++) {
787 if (!rb_equal(RSTRUCT_PTR(s
)[i
], RSTRUCT_PTR(s2
)[i
])) return Qfalse
;
794 * struct.hash => fixnum
796 * Return a hash value based on this struct's contents.
800 rb_struct_hash(VALUE s
)
805 h
= rb_hash(rb_obj_class(s
));
806 for (i
= 0; i
< RSTRUCT_LEN(s
); i
++) {
807 h
= (h
<< 1) | (h
<0 ? 1 : 0);
808 n
= rb_hash(RSTRUCT_PTR(s
)[i
]);
816 * struct.eql?(other) => true or false
818 * Two structures are equal if they are the same object, or if all their
819 * fields are equal (using <code>eql?</code>).
823 rb_struct_eql(VALUE s
, VALUE s2
)
827 if (s
== s2
) return Qtrue
;
828 if (TYPE(s2
) != T_STRUCT
) return Qfalse
;
829 if (rb_obj_class(s
) != rb_obj_class(s2
)) return Qfalse
;
830 if (RSTRUCT_LEN(s
) != RSTRUCT_LEN(s2
)) {
831 rb_bug("inconsistent struct"); /* should never happen */
834 for (i
=0; i
<RSTRUCT_LEN(s
); i
++) {
835 if (!rb_eql(RSTRUCT_PTR(s
)[i
], RSTRUCT_PTR(s2
)[i
])) return Qfalse
;
842 * struct.length => fixnum
843 * struct.size => fixnum
845 * Returns the number of instance variables.
847 * Customer = Struct.new(:name, :address, :zip)
848 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
853 rb_struct_size(VALUE s
)
855 return LONG2FIX(RSTRUCT_LEN(s
));
859 * A <code>Struct</code> is a convenient way to bundle a number of
860 * attributes together, using accessor methods, without having to write
863 * The <code>Struct</code> class is a generator of specific classes,
864 * each one of which is defined to hold a set of variables and their
865 * accessors. In these examples, we'll call the generated class
866 * ``<i>Customer</i>Class,'' and we'll show an example instance of that
867 * class as ``<i>Customer</i>Inst.''
869 * In the descriptions that follow, the parameter <i>symbol</i> refers
870 * to a symbol, which is either a quoted string or a
871 * <code>Symbol</code> (such as <code>:name</code>).
876 rb_cStruct
= rb_define_class("Struct", rb_cObject
);
877 rb_include_module(rb_cStruct
, rb_mEnumerable
);
879 rb_undef_alloc_func(rb_cStruct
);
880 rb_define_singleton_method(rb_cStruct
, "new", rb_struct_s_def
, -1);
882 rb_define_method(rb_cStruct
, "initialize", rb_struct_initialize
, -2);
883 rb_define_method(rb_cStruct
, "initialize_copy", rb_struct_init_copy
, 1);
885 rb_define_method(rb_cStruct
, "==", rb_struct_equal
, 1);
886 rb_define_method(rb_cStruct
, "eql?", rb_struct_eql
, 1);
887 rb_define_method(rb_cStruct
, "hash", rb_struct_hash
, 0);
889 rb_define_method(rb_cStruct
, "to_s", rb_struct_inspect
, 0);
890 rb_define_method(rb_cStruct
, "inspect", rb_struct_inspect
, 0);
891 rb_define_method(rb_cStruct
, "to_a", rb_struct_to_a
, 0);
892 rb_define_method(rb_cStruct
, "values", rb_struct_to_a
, 0);
893 rb_define_method(rb_cStruct
, "size", rb_struct_size
, 0);
894 rb_define_method(rb_cStruct
, "length", rb_struct_size
, 0);
896 rb_define_method(rb_cStruct
, "each", rb_struct_each
, 0);
897 rb_define_method(rb_cStruct
, "each_pair", rb_struct_each_pair
, 0);
898 rb_define_method(rb_cStruct
, "[]", rb_struct_aref
, 1);
899 rb_define_method(rb_cStruct
, "[]=", rb_struct_aset
, 2);
900 rb_define_method(rb_cStruct
, "select", rb_struct_select
, -1);
901 rb_define_method(rb_cStruct
, "values_at", rb_struct_values_at
, -1);
903 rb_define_method(rb_cStruct
, "members", rb_struct_members_m
, 0);