Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 922 lines (812 sloc) 23.458 kb
fedf489 2000-05-01
matz authored
1 /**********************************************************************
3db12e8 Initial revision
matz authored
2
3 struct.c -
4
5 $Author$
6 $Date$
7 created at: Tue Mar 22 18:44:30 JST 1995
8
9df466b Updated Copyrights of Matz to 2003.
michal authored
9 Copyright (C) 1993-2003 Yukihiro Matsumoto
fedf489 2000-05-01
matz authored
10
11 **********************************************************************/
3db12e8 Initial revision
matz authored
12
13 #include "ruby.h"
14
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
15 VALUE rb_cStruct;
3db12e8 Initial revision
matz authored
16
1fe40b7 * marshal.c (r_object): better allocation type check for
matz authored
17 static VALUE struct_alloc _((VALUE));
65a5162 1.4.0
matz authored
18
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
19 VALUE
20 rb_struct_iv_get(c, name)
21 VALUE c;
65a5162 1.4.0
matz authored
22 char *name;
23 {
24 ID id;
25
26 id = rb_intern(name);
27 for (;;) {
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
28 if (rb_ivar_defined(c, id))
29 return rb_ivar_get(c, id);
30 c = RCLASS(c)->super;
31 if (c == 0 || c == rb_cStruct)
65a5162 1.4.0
matz authored
32 return Qnil;
33 }
34 }
35
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
36 VALUE
37 rb_struct_s_members(klass)
08f84dc * struct.c (rb_struct_s_members): wrong call of struct_members.
matz authored
38 VALUE klass;
39 {
40 VALUE members = rb_struct_iv_get(klass, "__members__");
41
42 if (NIL_P(members)) {
43 rb_bug("non-initialized struct");
44 }
45 return members;
46 }
47
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
48 VALUE
49 rb_struct_members(s)
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
50 VALUE s;
51 {
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
52 VALUE members = rb_struct_s_members(rb_obj_class(s));
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
53
54 if (RSTRUCT(s)->len != RARRAY(members)->len) {
55 rb_raise(rb_eTypeError, "struct size differs (%d required %d given)",
56 RARRAY(members)->len, RSTRUCT(s)->len);
57 }
58 return members;
59 }
60
61 static VALUE
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
62 rb_struct_s_members_m(klass)
08f84dc * struct.c (rb_struct_s_members): wrong call of struct_members.
matz authored
63 VALUE klass;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
64 {
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
65 VALUE members, ary;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
66 VALUE *p, *pend;
67
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
68 members = rb_struct_s_members(klass);
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
69 ary = rb_ary_new2(RARRAY(members)->len);
70 p = RARRAY(members)->ptr; pend = p + RARRAY(members)->len;
3db12e8 Initial revision
matz authored
71 while (p < pend) {
7194267 2000-04-10
matz authored
72 rb_ary_push(ary, rb_str_new2(rb_id2name(SYM2ID(*p))));
3db12e8 Initial revision
matz authored
73 p++;
74 }
75
76 return ary;
77 }
78
8d32798 RDoc Struct and random
dave authored
79 /*
80 * call-seq:
81 * struct.members => array
82 *
83 * Returns an array of strings representing the names of the instance
84 * variables.
85 *
86 * Customer = Struct.new(:name, :address, :zip)
87 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
88 * joe.members #=> ["name", "address", "zip"]
89 */
90
3db12e8 Initial revision
matz authored
91 static VALUE
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
92 rb_struct_members_m(obj)
3db12e8 Initial revision
matz authored
93 VALUE obj;
94 {
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
95 return rb_struct_s_members_m(rb_obj_class(obj));
3db12e8 Initial revision
matz authored
96 }
97
98 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
99 rb_struct_getmember(obj, id)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
100 VALUE obj;
3db12e8 Initial revision
matz authored
101 ID id;
102 {
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
103 VALUE members, slot;
65a5162 1.4.0
matz authored
104 long i;
3db12e8 Initial revision
matz authored
105
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
106 members = rb_struct_members(obj);
8b1de0b 2000-05-24
matz authored
107 slot = ID2SYM(id);
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
108 for (i=0; i<RARRAY(members)->len; i++) {
109 if (RARRAY(members)->ptr[i] == slot) {
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
110 return RSTRUCT(obj)->ptr[i];
3db12e8 Initial revision
matz authored
111 }
112 }
ffe1cf5 * error.c (exc_exception): clone the receiver exception instead of
matz authored
113 rb_name_error(id, "%s is not struct member", rb_id2name(id));
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
114 return Qnil; /* not reached */
3db12e8 Initial revision
matz authored
115 }
116
117 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
118 rb_struct_ref(obj)
3db12e8 Initial revision
matz authored
119 VALUE obj;
120 {
70bbad3 * array.c: replace rb_protect_inspect() and rb_inspecting_p() by
matz authored
121 return rb_struct_getmember(obj, rb_frame_this_func());
3db12e8 Initial revision
matz authored
122 }
123
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
124 static VALUE rb_struct_ref0(obj) VALUE obj; {return RSTRUCT(obj)->ptr[0];}
125 static VALUE rb_struct_ref1(obj) VALUE obj; {return RSTRUCT(obj)->ptr[1];}
126 static VALUE rb_struct_ref2(obj) VALUE obj; {return RSTRUCT(obj)->ptr[2];}
127 static VALUE rb_struct_ref3(obj) VALUE obj; {return RSTRUCT(obj)->ptr[3];}
128 static VALUE rb_struct_ref4(obj) VALUE obj; {return RSTRUCT(obj)->ptr[4];}
129 static VALUE rb_struct_ref5(obj) VALUE obj; {return RSTRUCT(obj)->ptr[5];}
130 static VALUE rb_struct_ref6(obj) VALUE obj; {return RSTRUCT(obj)->ptr[6];}
131 static VALUE rb_struct_ref7(obj) VALUE obj; {return RSTRUCT(obj)->ptr[7];}
132 static VALUE rb_struct_ref8(obj) VALUE obj; {return RSTRUCT(obj)->ptr[8];}
133 static VALUE rb_struct_ref9(obj) VALUE obj; {return RSTRUCT(obj)->ptr[9];}
3db12e8 Initial revision
matz authored
134
65a5162 1.4.0
matz authored
135 static VALUE (*ref_func[10])() = {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
136 rb_struct_ref0,
137 rb_struct_ref1,
138 rb_struct_ref2,
139 rb_struct_ref3,
140 rb_struct_ref4,
141 rb_struct_ref5,
142 rb_struct_ref6,
143 rb_struct_ref7,
144 rb_struct_ref8,
145 rb_struct_ref9,
3db12e8 Initial revision
matz authored
146 };
147
14cd947 * struct.c (rb_struct_modify): should check frozen and taint
matz authored
148 static void
149 rb_struct_modify(s)
150 VALUE s;
151 {
152 if (OBJ_FROZEN(s)) rb_error_frozen("Struct");
153 if (!OBJ_TAINTED(s) && rb_safe_level() >= 4)
154 rb_raise(rb_eSecurityError, "Insecure: can't modify Struct");
155 }
156
3db12e8 Initial revision
matz authored
157 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
158 rb_struct_set(obj, val)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
159 VALUE obj, val;
3db12e8 Initial revision
matz authored
160 {
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
161 VALUE members, slot;
65a5162 1.4.0
matz authored
162 long i;
3db12e8 Initial revision
matz authored
163
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
164 members = rb_struct_members(obj);
14cd947 * struct.c (rb_struct_modify): should check frozen and taint
matz authored
165 rb_struct_modify(obj);
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
166 for (i=0; i<RARRAY(members)->len; i++) {
167 slot = RARRAY(members)->ptr[i];
70bbad3 * array.c: replace rb_protect_inspect() and rb_inspecting_p() by
matz authored
168 if (rb_id_attrset(SYM2ID(slot)) == rb_frame_this_func()) {
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
169 return RSTRUCT(obj)->ptr[i] = val;
3db12e8 Initial revision
matz authored
170 }
171 }
70bbad3 * array.c: replace rb_protect_inspect() and rb_inspecting_p() by
matz authored
172 rb_name_error(rb_frame_this_func(), "`%s' is not a struct member",
173 rb_id2name(rb_frame_this_func()));
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
174 return Qnil; /* not reached */
3db12e8 Initial revision
matz authored
175 }
176
177 static VALUE
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
178 make_struct(name, members, klass)
179 VALUE name, members, klass;
3db12e8 Initial revision
matz authored
180 {
181 VALUE nstr;
182 ID id;
65a5162 1.4.0
matz authored
183 long i;
3db12e8 Initial revision
matz authored
184
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
185 OBJ_FREEZE(members);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
186 if (NIL_P(name)) {
187 nstr = rb_class_new(klass);
e194918 @nobu * struct.c (make_struct): needs meta class.
nobu authored
188 rb_make_metaclass(nstr, RBASIC(klass)->klass);
5915dc2 @nobu * class.c (rb_make_metaclass): [new]
nobu authored
189 rb_class_inherited(klass, nstr);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
190 }
191 else {
fd06a2a * eval.c (block_pass): should not downgrade safe level.
matz authored
192 char *cname = StringValuePtr(name);
70bbad3 * array.c: replace rb_protect_inspect() and rb_inspecting_p() by
matz authored
193
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
194 id = rb_intern(cname);
9d228b1 19991214
matz authored
195 if (!rb_is_const_id(id)) {
ffe1cf5 * error.c (exc_exception): clone the receiver exception instead of
matz authored
196 rb_name_error(id, "identifier %s needs to be constant", cname);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
197 }
880d7af * struct.c (make_struct): remove redefining constant when
matz authored
198 if (rb_const_defined_at(klass, id)) {
199 rb_warn("redefining constant Struct::%s", cname);
200 rb_mod_remove_const(klass, ID2SYM(id));
201 }
70bbad3 * array.c: replace rb_protect_inspect() and rb_inspecting_p() by
matz authored
202 nstr = rb_define_class_under(klass, rb_id2name(id), klass);
3db12e8 Initial revision
matz authored
203 }
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
204 rb_iv_set(nstr, "__size__", LONG2NUM(RARRAY(members)->len));
205 rb_iv_set(nstr, "__members__", members);
3db12e8 Initial revision
matz authored
206
bc49bc7 * parse.y (do_block): split "do" block and tLBRACE_ARG block.
matz authored
207 rb_define_alloc_func(nstr, struct_alloc);
1fe40b7 * marshal.c (r_object): better allocation type check for
matz authored
208 rb_define_singleton_method(nstr, "new", rb_class_new_instance, -1);
209 rb_define_singleton_method(nstr, "[]", rb_class_new_instance, -1);
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
210 rb_define_singleton_method(nstr, "members", rb_struct_s_members_m, 0);
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
211 for (i=0; i< RARRAY(members)->len; i++) {
212 ID id = SYM2ID(RARRAY(members)->ptr[i]);
ce3eb0b * eval.c (rb_attr): attribute name check added.
matz authored
213 if (rb_is_local_id(id) || rb_is_const_id(id)) {
56b9f4d * struct.c (make_struct): allow non local-id field
matz authored
214 if (i<sizeof(ref_func)) {
215 rb_define_method_id(nstr, id, ref_func[i], 0);
216 }
217 else {
218 rb_define_method_id(nstr, id, rb_struct_ref, 0);
219 }
220 rb_define_method_id(nstr, rb_id_attrset(id), rb_struct_set, 1);
70bbad3 * array.c: replace rb_protect_inspect() and rb_inspecting_p() by
matz authored
221 }
3db12e8 Initial revision
matz authored
222 }
223
224 return nstr;
225 }
226
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
227 #ifdef HAVE_STDARG_PROTOTYPES
228 #include <stdarg.h>
229 #define va_init_list(a,b) va_start(a,b)
230 #else
3db12e8 Initial revision
matz authored
231 #include <varargs.h>
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
232 #define va_init_list(a,b) va_start(a)
233 #endif
3db12e8 Initial revision
matz authored
234
235 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
236 #ifdef HAVE_STDARG_PROTOTYPES
65a5162 1.4.0
matz authored
237 rb_struct_define(const char *name, ...)
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
238 #else
239 rb_struct_define(name, va_alist)
65a5162 1.4.0
matz authored
240 const char *name;
3db12e8 Initial revision
matz authored
241 va_dcl
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
242 #endif
3db12e8 Initial revision
matz authored
243 {
244 va_list ar;
245 VALUE nm, ary;
246 char *mem;
247
9d228b1 19991214
matz authored
248 if (!name) nm = Qnil;
249 else nm = rb_str_new2(name);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
250 ary = rb_ary_new();
3db12e8 Initial revision
matz authored
251
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
252 va_init_list(ar, name);
3db12e8 Initial revision
matz authored
253 while (mem = va_arg(ar, char*)) {
254 ID slot = rb_intern(mem);
7194267 2000-04-10
matz authored
255 rb_ary_push(ary, ID2SYM(slot));
3db12e8 Initial revision
matz authored
256 }
257 va_end(ar);
258
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
259 return make_struct(nm, ary, rb_cStruct);
3db12e8 Initial revision
matz authored
260 }
261
8d32798 RDoc Struct and random
dave authored
262 /*
263 * call-seq:
264 * Struct.new( [aString] [, aSym]+> ) => StructClass
265 * StructClass.new(arg, ...) => obj
266 * StructClass[arg, ...] => obj
267 *
268 * Creates a new class, named by <i>aString</i>, containing accessor
269 * methods for the given symbols. If the name <i>aString</i> is
270 * omitted, an anonymous structure class will be created. Otherwise,
271 * the name of this struct will appear as a constant in class
272 * <code>Struct</code>, so it must be unique for all
273 * <code>Struct</code>s in the system and should start with a capital
274 * letter. Assigning a structure class to a constant effectively gives
275 * the class the name of the constant.
276 *
277 * <code>Struct::new</code> returns a new <code>Class</code> object,
278 * which can then be used to create specific instances of the new
279 * structure. The number of actual parameters must be
280 * less than or equal to the number of attributes defined for this
281 * class; unset parameters default to \nil{}. Passing too many
282 * parameters will raise an \E{ArgumentError}.
283 *
284 * The remaining methods listed in this section (class and instance)
285 * are defined for this generated class.
286 *
287 * # Create a structure with a name in Struct
288 * Struct.new("Customer", :name, :address) #=> Struct::Customer
289 * Struct::Customer.new("Dave", "123 Main") #=> #<Struct::Customer name="Dave", address="123 Main">
290 *
291 * # Create a structure named by its constant
292 * Customer = Struct.new(:name, :address) #=> Customer
293 * Customer.new("Dave", "123 Main") #=> #<Customer name="Dave", address="123 Main">
294 */
295
3db12e8 Initial revision
matz authored
296 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
297 rb_struct_s_def(argc, argv, klass)
3db12e8 Initial revision
matz authored
298 int argc;
299 VALUE *argv;
9d228b1 19991214
matz authored
300 VALUE klass;
3db12e8 Initial revision
matz authored
301 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
302 VALUE name, rest;
65a5162 1.4.0
matz authored
303 long i;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
304 VALUE st;
9d228b1 19991214
matz authored
305 ID id;
3db12e8 Initial revision
matz authored
306
307 rb_scan_args(argc, argv, "1*", &name, &rest);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
308 for (i=0; i<RARRAY(rest)->len; i++) {
9d228b1 19991214
matz authored
309 id = rb_to_id(RARRAY(rest)->ptr[i]);
7194267 2000-04-10
matz authored
310 RARRAY(rest)->ptr[i] = ID2SYM(id);
3db12e8 Initial revision
matz authored
311 }
7c097dc * struct.c (rb_struct_s_def): Struct::new executes block with
matz authored
312 if (!NIL_P(name)) {
313 VALUE tmp = rb_check_string_type(name);
314
315 if (NIL_P(tmp)) {
316 id = rb_to_id(name);
317 rb_ary_unshift(rest, ID2SYM(id));
318 name = Qnil;
319 }
9d228b1 19991214
matz authored
320 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
321 st = make_struct(name, rest, klass);
7c097dc * struct.c (rb_struct_s_def): Struct::new executes block with
matz authored
322 if (rb_block_given_p()) {
323 rb_mod_module_eval(0, 0, st);
324 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
325
326 return st;
3db12e8 Initial revision
matz authored
327 }
328
8d32798 RDoc Struct and random
dave authored
329 /*
330 */
331
65a5162 1.4.0
matz authored
332 static VALUE
333 rb_struct_initialize(self, values)
334 VALUE self, values;
3db12e8 Initial revision
matz authored
335 {
c786866 * range.c (range_step): 'iter' here should be an array.
matz authored
336 VALUE klass = rb_obj_class(self);
3db12e8 Initial revision
matz authored
337 VALUE size;
65a5162 1.4.0
matz authored
338 long n;
3db12e8 Initial revision
matz authored
339
14cd947 * struct.c (rb_struct_modify): should check frozen and taint
matz authored
340 rb_struct_modify(self);
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
341 size = rb_struct_iv_get(klass, "__size__");
7194267 2000-04-10
matz authored
342 n = FIX2LONG(size);
65a5162 1.4.0
matz authored
343 if (n < RARRAY(values)->len) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
344 rb_raise(rb_eArgError, "struct size differs");
3db12e8 Initial revision
matz authored
345 }
65a5162 1.4.0
matz authored
346 MEMCPY(RSTRUCT(self)->ptr, RARRAY(values)->ptr, VALUE, RARRAY(values)->len);
347 if (n > RARRAY(values)->len) {
348 rb_mem_clear(RSTRUCT(self)->ptr+RARRAY(values)->len,
349 n-RARRAY(values)->len);
3db12e8 Initial revision
matz authored
350 }
65a5162 1.4.0
matz authored
351 return Qnil;
352 }
353
354 static VALUE
1fe40b7 * marshal.c (r_object): better allocation type check for
matz authored
355 struct_alloc(klass)
65a5162 1.4.0
matz authored
356 VALUE klass;
357 {
358 VALUE size;
359 long n;
360 NEWOBJ(st, struct RStruct);
361 OBJSETUP(st, klass, T_STRUCT);
362
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
363 size = rb_struct_iv_get(klass, "__size__");
65a5162 1.4.0
matz authored
364 n = FIX2LONG(size);
365
366 st->ptr = ALLOC_N(VALUE, n);
367 rb_mem_clear(st->ptr, n);
368 st->len = n;
369
370 return (VALUE)st;
371 }
372
373 VALUE
374 rb_struct_alloc(klass, values)
375 VALUE klass, values;
376 {
1fe40b7 * marshal.c (r_object): better allocation type check for
matz authored
377 return rb_class_new_instance(RARRAY(values)->len, RARRAY(values)->ptr, klass);
3db12e8 Initial revision
matz authored
378 }
379
380 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
381 #ifdef HAVE_STDARG_PROTOTYPES
382 rb_struct_new(VALUE klass, ...)
383 #else
384 rb_struct_new(klass, va_alist)
385 VALUE klass;
3db12e8 Initial revision
matz authored
386 va_dcl
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
387 #endif
3db12e8 Initial revision
matz authored
388 {
65a5162 1.4.0
matz authored
389 VALUE sz, *mem;
390 long size, i;
3db12e8 Initial revision
matz authored
391 va_list args;
392
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
393 sz = rb_struct_iv_get(klass, "__size__");
65a5162 1.4.0
matz authored
394 size = FIX2LONG(sz);
395 mem = ALLOCA_N(VALUE, size);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
396 va_init_list(args, klass);
397 for (i=0; i<size; i++) {
65a5162 1.4.0
matz authored
398 mem[i] = va_arg(args, VALUE);
3db12e8 Initial revision
matz authored
399 }
400 va_end(args);
401
1fe40b7 * marshal.c (r_object): better allocation type check for
matz authored
402 return rb_class_new_instance(size, mem, klass);
3db12e8 Initial revision
matz authored
403 }
404
8d32798 RDoc Struct and random
dave authored
405 /*
406 * call-seq:
407 * struct.each {|obj| block } => struct
408 *
409 * Calls <i>block</i> once for each instance variable, passing the
410 * value as a parameter.
411 *
412 * Customer = Struct.new(:name, :address, :zip)
413 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
414 * joe.each {|x| puts(x) }
415 *
416 * <em>produces:</em>
417 *
418 * Joe Smith
419 * 123 Maple, Anytown NC
420 * 12345
421 */
422
3db12e8 Initial revision
matz authored
423 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
424 rb_struct_each(s)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
425 VALUE s;
3db12e8 Initial revision
matz authored
426 {
65a5162 1.4.0
matz authored
427 long i;
3db12e8 Initial revision
matz authored
428
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
429 for (i=0; i<RSTRUCT(s)->len; i++) {
430 rb_yield(RSTRUCT(s)->ptr[i]);
3db12e8 Initial revision
matz authored
431 }
65a5162 1.4.0
matz authored
432 return s;
3db12e8 Initial revision
matz authored
433 }
434
8d32798 RDoc Struct and random
dave authored
435 /*
436 * call-seq:
437 * struct.each_pair {|sym, obj| block } => struct
438 *
439 * Calls <i>block</i> once for each instance variable, passing the name
440 * (as a symbol) and the value as parameters.
441 *
442 * Customer = Struct.new(:name, :address, :zip)
443 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
444 * joe.each_pair {|name, value| puts("#{name} => #{value}") }
445 *
446 * <em>produces:</em>
447 *
448 * name => Joe Smith
449 * address => 123 Maple, Anytown NC
450 * zip => 12345
451 */
452
3db12e8 Initial revision
matz authored
453 static VALUE
19c42c0 * variable.c (rb_obj_remove_instance_variable): raise NameError if
matz authored
454 rb_struct_each_pair(s)
455 VALUE s;
456 {
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
457 VALUE members;
19c42c0 * variable.c (rb_obj_remove_instance_variable): raise NameError if
matz authored
458 long i;
459
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
460 members = rb_struct_members(s);
19c42c0 * variable.c (rb_obj_remove_instance_variable): raise NameError if
matz authored
461 for (i=0; i<RSTRUCT(s)->len; i++) {
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
462 rb_yield_values(2, rb_ary_entry(members, i), RSTRUCT(s)->ptr[i]);
19c42c0 * variable.c (rb_obj_remove_instance_variable): raise NameError if
matz authored
463 }
464 return s;
465 }
466
467 static VALUE
70bbad3 * array.c: replace rb_protect_inspect() and rb_inspecting_p() by
matz authored
468 inspect_struct(s, dummy, recur)
469 VALUE s, dummy;
470 int recur;
3db12e8 Initial revision
matz authored
471 {
c786866 * range.c (range_step): 'iter' here should be an array.
matz authored
472 char *cname = rb_class2name(rb_obj_class(s));
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
473 VALUE str, members;
65a5162 1.4.0
matz authored
474 long i;
3db12e8 Initial revision
matz authored
475
70bbad3 * array.c: replace rb_protect_inspect() and rb_inspecting_p() by
matz authored
476 if (recur) {
477 char *cname = rb_class2name(rb_obj_class(s));
478 VALUE str = rb_str_new(0, strlen(cname) + 15);
479
480 sprintf(RSTRING(str)->ptr, "#<struct %s:...>", cname);
481 RSTRING(str)->len = strlen(RSTRING(str)->ptr);
482 return str;
483 }
484
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
485 members = rb_struct_members(s);
9d22a06 * array.c (rb_values_at): extract common procedure from
matz authored
486 str = rb_str_buf_new2("#<struct ");
7194267 2000-04-10
matz authored
487 rb_str_cat2(str, cname);
488 rb_str_cat2(str, " ");
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
489 for (i=0; i<RSTRUCT(s)->len; i++) {
56b9f4d * struct.c (make_struct): allow non local-id field
matz authored
490 VALUE slot;
491 ID id;
3db12e8 Initial revision
matz authored
492 char *p;
493
494 if (i > 0) {
7194267 2000-04-10
matz authored
495 rb_str_cat2(str, ", ");
3db12e8 Initial revision
matz authored
496 }
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
497 slot = RARRAY(members)->ptr[i];
56b9f4d * struct.c (make_struct): allow non local-id field
matz authored
498 id = SYM2ID(slot);
ce3eb0b * eval.c (rb_attr): attribute name check added.
matz authored
499 if (rb_is_local_id(id) || rb_is_const_id(id)) {
56b9f4d * struct.c (make_struct): allow non local-id field
matz authored
500 p = rb_id2name(id);
501 rb_str_cat2(str, p);
502 }
503 else {
504 rb_str_append(str, rb_inspect(slot));
505 }
7194267 2000-04-10
matz authored
506 rb_str_cat2(str, "=");
56b9f4d * struct.c (make_struct): allow non local-id field
matz authored
507 rb_str_append(str, rb_inspect(RSTRUCT(s)->ptr[i]));
3db12e8 Initial revision
matz authored
508 }
7194267 2000-04-10
matz authored
509 rb_str_cat2(str, ">");
bf70582 2000-02-23
matz authored
510 OBJ_INFECT(str, s);
3db12e8 Initial revision
matz authored
511
512 return str;
513 }
514
8d32798 RDoc Struct and random
dave authored
515 /*
516 * call-seq:
517 * struct.to_s => string
518 * struct.inspect => string
519 *
520 * Describe the contents of this struct in a string.
521 */
522
3db12e8 Initial revision
matz authored
523 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
524 rb_struct_inspect(s)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
525 VALUE s;
3db12e8 Initial revision
matz authored
526 {
70bbad3 * array.c: replace rb_protect_inspect() and rb_inspecting_p() by
matz authored
527 return rb_exec_recursive(inspect_struct, s, 0);
3db12e8 Initial revision
matz authored
528 }
529
8d32798 RDoc Struct and random
dave authored
530 /*
531 * call-seq:
532 * struct.to_a => array
533 * struct.values => array
534 *
535 * Returns the values for this instance as an array.
536 *
537 * Customer = Struct.new(:name, :address, :zip)
538 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
539 * joe.to_a[1] #=> "123 Maple, Anytown NC"
540 */
541
3db12e8 Initial revision
matz authored
542 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
543 rb_struct_to_a(s)
544 VALUE s;
545 {
546 return rb_ary_new4(RSTRUCT(s)->len, RSTRUCT(s)->ptr);
547 }
548
a7b1a2e @nobu * class.c, error.c, file.c, io.c, numeric.c, object.c, re.c, struct.c,
nobu authored
549 /* :nodoc: */
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
550 static VALUE
6e52d10 * object.c (init_copy): rename copy_object as initialize_copy,
matz authored
551 rb_struct_init_copy(copy, s)
6f484e4 * variable.c (rb_copy_generic_ivar): remove old generic instance
matz authored
552 VALUE copy, s;
3db12e8 Initial revision
matz authored
553 {
6f484e4 * variable.c (rb_copy_generic_ivar): remove old generic instance
matz authored
554 if (copy == s) return copy;
555 rb_check_frozen(copy);
556 if (!rb_obj_is_instance_of(s, rb_obj_class(copy))) {
40bc4f5 * array.c (rb_ary_become): should not free ptr if it's shared.
matz authored
557 rb_raise(rb_eTypeError, "wrong argument class");
c45908e * file.c (rb_find_file): $LOAD_PATH must not be empty.
matz authored
558 }
6f484e4 * variable.c (rb_copy_generic_ivar): remove old generic instance
matz authored
559 RSTRUCT(copy)->ptr = ALLOC_N(VALUE, RSTRUCT(s)->len);
560 RSTRUCT(copy)->len = RSTRUCT(s)->len;
561 MEMCPY(RSTRUCT(copy)->ptr, RSTRUCT(s)->ptr, VALUE, RSTRUCT(copy)->len);
de71615 20000105
matz authored
562
6f484e4 * variable.c (rb_copy_generic_ivar): remove old generic instance
matz authored
563 return copy;
3db12e8 Initial revision
matz authored
564 }
565
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
566 static VALUE
567 rb_struct_aref_id(s, id)
568 VALUE s;
569 ID id;
570 {
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
571 VALUE members;
65a5162 1.4.0
matz authored
572 long i, len;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
573
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
574 members = rb_struct_members(s);
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
575 len = RARRAY(members)->len;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
576 for (i=0; i<len; i++) {
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
577 if (SYM2ID(RARRAY(members)->ptr[i]) == id) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
578 return RSTRUCT(s)->ptr[i];
579 }
580 }
ffe1cf5 * error.c (exc_exception): clone the receiver exception instead of
matz authored
581 rb_name_error(id, "no member '%s' in struct", rb_id2name(id));
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
582 return Qnil; /* not reached */
583 }
584
8d32798 RDoc Struct and random
dave authored
585 /*
586 * call-seq:
587 * struct[symbol] => anObject
588 * struct[fixnum] => anObject
589 *
590 * Attribute Reference---Returns the value of the instance variable
591 * named by <i>symbol</i>, or indexed (0..length-1) by
592 * <i>fixnum</i>. Will raise <code>NameError</code> if the named
593 * variable does not exist, or <code>IndexError</code> if the index is
594 * out of range.
595 *
596 * Customer = Struct.new(:name, :address, :zip)
597 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
598 *
599 * joe["name"] #=> "Joe Smith"
600 * joe[:name] #=> "Joe Smith"
601 * joe[0] #=> "Joe Smith"
602 */
603
3db12e8 Initial revision
matz authored
604 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
605 rb_struct_aref(s, idx)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
606 VALUE s, idx;
3db12e8 Initial revision
matz authored
607 {
65a5162 1.4.0
matz authored
608 long i;
3db12e8 Initial revision
matz authored
609
31c5530 2000-03-13
matz authored
610 if (TYPE(idx) == T_STRING || TYPE(idx) == T_SYMBOL) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
611 return rb_struct_aref_id(s, rb_to_id(idx));
612 }
613
65a5162 1.4.0
matz authored
614 i = NUM2LONG(idx);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
615 if (i < 0) i = RSTRUCT(s)->len + i;
3db12e8 Initial revision
matz authored
616 if (i < 0)
a2868ff @knu * eval.c, file.c, gc.c, io.c, object.c, ruby.c, ruby.h, struct.c,
knu authored
617 rb_raise(rb_eIndexError, "offset %ld too small for struct(size:%ld)",
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
618 i, RSTRUCT(s)->len);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
619 if (RSTRUCT(s)->len <= i)
a2868ff @knu * eval.c, file.c, gc.c, io.c, object.c, ruby.c, ruby.h, struct.c,
knu authored
620 rb_raise(rb_eIndexError, "offset %ld too large for struct(size:%ld)",
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
621 i, RSTRUCT(s)->len);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
622 return RSTRUCT(s)->ptr[i];
3db12e8 Initial revision
matz authored
623 }
624
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
625 static VALUE
626 rb_struct_aset_id(s, id, val)
627 VALUE s, val;
628 ID id;
629 {
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
630 VALUE members;
65a5162 1.4.0
matz authored
631 long i, len;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
632
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
633 members = rb_struct_members(s);
14cd947 * struct.c (rb_struct_modify): should check frozen and taint
matz authored
634 rb_struct_modify(s);
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
635 len = RARRAY(members)->len;
636 if (RSTRUCT(s)->len != RARRAY(members)->len) {
637 rb_raise(rb_eTypeError, "struct size differs (%d required %d given)",
638 RARRAY(members)->len, RSTRUCT(s)->len);
639 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
640 for (i=0; i<len; i++) {
c0a998d * parse.y (rb_parser_append_print): should handle prelude.
matz authored
641 if (SYM2ID(RARRAY(members)->ptr[i]) == id) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
642 RSTRUCT(s)->ptr[i] = val;
643 return val;
644 }
645 }
ffe1cf5 * error.c (exc_exception): clone the receiver exception instead of
matz authored
646 rb_name_error(id, "no member '%s' in struct", rb_id2name(id));
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
647 }
648
8d32798 RDoc Struct and random
dave authored
649 /*
650 * call-seq:
651 * struct[symbol] = obj => obj
652 * struct[fixnum] = obj => obj
653 *
654 * Attribute Assignment---Assigns to the instance variable named by
655 * <i>symbol</i> or <i>fixnum</i> the value <i>obj</i> and
656 * returns it. Will raise a <code>NameError</code> if the named
657 * variable does not exist, or an <code>IndexError</code> if the index
658 * is out of range.
659 *
660 * Customer = Struct.new(:name, :address, :zip)
661 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
662 *
663 * joe["name"] = "Luke"
664 * joe[:zip] = "90210"
665 *
666 * joe.name #=> "Luke"
667 * joe.zip #=> "90210"
668 */
669
3db12e8 Initial revision
matz authored
670 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
671 rb_struct_aset(s, idx, val)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
672 VALUE s, idx, val;
3db12e8 Initial revision
matz authored
673 {
65a5162 1.4.0
matz authored
674 long i;
3db12e8 Initial revision
matz authored
675
31c5530 2000-03-13
matz authored
676 if (TYPE(idx) == T_STRING || TYPE(idx) == T_SYMBOL) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
677 return rb_struct_aset_id(s, rb_to_id(idx), val);
678 }
679
65a5162 1.4.0
matz authored
680 i = NUM2LONG(idx);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
681 if (i < 0) i = RSTRUCT(s)->len + i;
abc49e4 2000-06-14
matz authored
682 if (i < 0) {
a2868ff @knu * eval.c, file.c, gc.c, io.c, object.c, ruby.c, ruby.h, struct.c,
knu authored
683 rb_raise(rb_eIndexError, "offset %ld too small for struct(size:%ld)",
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
684 i, RSTRUCT(s)->len);
abc49e4 2000-06-14
matz authored
685 }
686 if (RSTRUCT(s)->len <= i) {
a2868ff @knu * eval.c, file.c, gc.c, io.c, object.c, ruby.c, ruby.h, struct.c,
knu authored
687 rb_raise(rb_eIndexError, "offset %ld too large for struct(size:%ld)",
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
688 i, RSTRUCT(s)->len);
abc49e4 2000-06-14
matz authored
689 }
14cd947 * struct.c (rb_struct_modify): should check frozen and taint
matz authored
690 rb_struct_modify(s);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
691 return RSTRUCT(s)->ptr[i] = val;
3db12e8 Initial revision
matz authored
692 }
693
dbb2e33 @unak * struct.c (struct_entry): add prototype to avoid VC++ warnings.
unak authored
694 static VALUE struct_entry _((VALUE, long));
f595d5b * array.c (rb_ary_values_at): new method to replace select(index..).
matz authored
695 static VALUE
9d22a06 * array.c (rb_values_at): extract common procedure from
matz authored
696 struct_entry(s, n)
697 VALUE s;
698 long n;
699 {
700 return rb_struct_aref(s, LONG2NUM(n));
701 }
702
8d32798 RDoc Struct and random
dave authored
703 /*
704 * call-seq:
705 * struct.values_at(selector,... ) => an_array
706 *
707 * Returns an array containing the elements in
708 * _self_ corresponding to the given selector(s). The selectors
709 * may be either integer indices or ranges.
710 * See also </code>.select<code>.
711 *
712 * a = %w{ a b c d e f }
713 * a.values_at(1, 3, 5)
714 * a.values_at(1, 3, 5, 7)
715 * a.values_at(-1, -3, -5, -7)
716 * a.values_at(1..3, 2...5)
717 */
718
9d22a06 * array.c (rb_values_at): extract common procedure from
matz authored
719 static VALUE
f595d5b * array.c (rb_ary_values_at): new method to replace select(index..).
matz authored
720 rb_struct_values_at(argc, argv, s)
721 int argc;
722 VALUE *argv;
723 VALUE s;
724 {
4ded52b * file.c (rb_get_path): get path string via "to_path" method if
matz authored
725 return rb_get_values_at(s, RSTRUCT(s)->len, argc, argv, struct_entry);
f595d5b * array.c (rb_ary_values_at): new method to replace select(index..).
matz authored
726 }
a59c599 * string.c (rb_str_match_m): should convert an argument into
matz authored
727
8d32798 RDoc Struct and random
dave authored
728 /*
729 * call-seq:
730 * struct.select(fixnum, ... ) => array
731 * struct.select {|i| block } => array
732 *
733 * The first form returns an array containing the elements in
734 * <i>struct</i> corresponding to the given indices. The second
735 * form invokes the block passing in successive elements from
736 * <i>struct</i>, returning an array containing those elements
737 * for which the block returns a true value (equivalent to
738 * <code>Enumerable#select</code>).
739 *
740 * Lots = Struct.new(:a, :b, :c, :d, :e, :f)
741 * l = Lots.new(11, 22, 33, 44, 55, 66)
742 * l.select(1, 3, 5) #=> [22, 44, 66]
743 * l.select(0, 2, 4) #=> [11, 33, 55]
744 * l.select(-1, -3, -5) #=> [66, 44, 22]
745 * l.select {|v| (v % 2).zero? } #=> [22, 44, 66]
746 */
747
a59c599 * string.c (rb_str_match_m): should convert an argument into
matz authored
748 static VALUE
749 rb_struct_select(argc, argv, s)
750 int argc;
751 VALUE *argv;
752 VALUE s;
753 {
f595d5b * array.c (rb_ary_values_at): new method to replace select(index..).
matz authored
754 VALUE result;
a59c599 * string.c (rb_str_match_m): should convert an argument into
matz authored
755 long i;
756
f595d5b * array.c (rb_ary_values_at): new method to replace select(index..).
matz authored
757 if (argc > 0) {
919f456 * exception message clean-up by Ian Macdonald <ian@caliban.org>.
matz authored
758 rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc);
f595d5b * array.c (rb_ary_values_at): new method to replace select(index..).
matz authored
759 }
760 result = rb_ary_new();
761 for (i = 0; i < RSTRUCT(s)->len; i++) {
762 if (RTEST(rb_yield(RSTRUCT(s)->ptr[i]))) {
763 rb_ary_push(result, RSTRUCT(s)->ptr[i]);
a59c599 * string.c (rb_str_match_m): should convert an argument into
matz authored
764 }
765 }
f595d5b * array.c (rb_ary_values_at): new method to replace select(index..).
matz authored
766
a59c599 * string.c (rb_str_match_m): should convert an argument into
matz authored
767 return result;
768 }
769
8d32798 RDoc Struct and random
dave authored
770 /*
771 * call-seq:
772 * struct == other_struct => true or false
773 *
774 * Equality---Returns <code>true</code> if <i>other_struct</i> is
775 * equal to this one: they must be of the same class as generated by
776 * <code>Struct::new</code>, and the values of all instance variables
777 * must be equal (according to <code>Object#==</code>).
778 *
779 * Customer = Struct.new(:name, :address, :zip)
780 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
781 * joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
782 * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345)
783 * joe == joejr #=> true
784 * joe == jane #=> false
785 */
786
3db12e8 Initial revision
matz authored
787 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
788 rb_struct_equal(s, s2)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
789 VALUE s, s2;
3db12e8 Initial revision
matz authored
790 {
65a5162 1.4.0
matz authored
791 long i;
3db12e8 Initial revision
matz authored
792
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
793 if (s == s2) return Qtrue;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
794 if (TYPE(s2) != T_STRUCT) return Qfalse;
c786866 * range.c (range_step): 'iter' here should be an array.
matz authored
795 if (rb_obj_class(s) != rb_obj_class(s2)) return Qfalse;
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
796 if (RSTRUCT(s)->len != RSTRUCT(s2)->len) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
797 rb_bug("inconsistent struct"); /* should never happen */
3db12e8 Initial revision
matz authored
798 }
799
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
800 for (i=0; i<RSTRUCT(s)->len; i++) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
801 if (!rb_equal(RSTRUCT(s)->ptr[i], RSTRUCT(s2)->ptr[i])) return Qfalse;
3db12e8 Initial revision
matz authored
802 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
803 return Qtrue;
3db12e8 Initial revision
matz authored
804 }
805
8d32798 RDoc Struct and random
dave authored
806 /*
807 * call-seq:
808 * struct.hash => fixnum
809 *
810 * Return a hash value based on this struct's contents.
811 */
812
3db12e8 Initial revision
matz authored
813 static VALUE
df61e2a @nobu * struct.c (rb_struct_hash): new methods Struct#hash, Struct#eql?.
nobu authored
814 rb_struct_hash(s)
815 VALUE s;
816 {
817 long i, h;
818 VALUE n;
819
820 h = rb_hash(rb_obj_class(s));
821 for (i = 0; i < RSTRUCT(s)->len; i++) {
822 h = (h << 1) | (h<0 ? 1 : 0);
823 n = rb_hash(RSTRUCT(s)->ptr[i]);
824 h ^= NUM2LONG(n);
825 }
826 return LONG2FIX(h);
827 }
828
8d32798 RDoc Struct and random
dave authored
829 /*
830 * code-seq:
831 * struct.eql?(other) => true or false
832 *
833 * Two structures are equal if they are the same object, or if all their
834 * fields are equal (using <code>eql?</code>).
835 */
836
df61e2a @nobu * struct.c (rb_struct_hash): new methods Struct#hash, Struct#eql?.
nobu authored
837 static VALUE
6987b08 * struct.c (rb_struct_eql): should compare values with "eql?".
matz authored
838 rb_struct_eql(s, s2)
839 VALUE s, s2;
840 {
841 long i;
842
843 if (s == s2) return Qtrue;
844 if (TYPE(s2) != T_STRUCT) return Qfalse;
845 if (rb_obj_class(s) != rb_obj_class(s2)) return Qfalse;
846 if (RSTRUCT(s)->len != RSTRUCT(s2)->len) {
847 rb_bug("inconsistent struct"); /* should never happen */
848 }
849
850 for (i=0; i<RSTRUCT(s)->len; i++) {
851 if (!rb_eql(RSTRUCT(s)->ptr[i], RSTRUCT(s2)->ptr[i])) return Qfalse;
852 }
853 return Qtrue;
854 }
855
8d32798 RDoc Struct and random
dave authored
856 /*
857 * call-seq:
858 * struct.length => fixnum
859 * struct.size => fixnum
860 *
861 * Returns the number of instance variables.
862 *
863 * Customer = Struct.new(:name, :address, :zip)
864 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
865 * joe.length #=> 3
866 */
867
6987b08 * struct.c (rb_struct_eql): should compare values with "eql?".
matz authored
868 static VALUE
c18d374 991207
matz authored
869 rb_struct_size(s)
870 VALUE s;
871 {
ffc13a6 *.c: Int vs Long cleanup
michal authored
872 return LONG2FIX(RSTRUCT(s)->len);
c18d374 991207
matz authored
873 }
874
8d32798 RDoc Struct and random
dave authored
875 /*
876 * A <code>Struct</code> is a convenient way to bundle a number of
877 * attributes together, using accessor methods, without having to write
878 * an explicit class.
879 *
880 * The <code>Struct</code> class is a generator of specific classes,
881 * each one of which is defined to hold a set of variables and their
882 * accessors. In these examples, we'll call the generated class
883 * ``<i>Customer</i>Class,'' and we'll show an example instance of that
884 * class as ``<i>Customer</i>Inst.''
885 *
886 * In the descriptions that follow, the parameter <i>symbol</i> refers
887 * to a symbol, which is either a quoted string or a
888 * <code>Symbol</code> (such as <code>:name</code>).
889 */
3db12e8 Initial revision
matz authored
890 void
891 Init_Struct()
892 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
893 rb_cStruct = rb_define_class("Struct", rb_cObject);
894 rb_include_module(rb_cStruct, rb_mEnumerable);
3db12e8 Initial revision
matz authored
895
bc49bc7 * parse.y (do_block): split "do" block and tLBRACE_ARG block.
matz authored
896 rb_undef_alloc_func(rb_cStruct);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
897 rb_define_singleton_method(rb_cStruct, "new", rb_struct_s_def, -1);
3db12e8 Initial revision
matz authored
898
65a5162 1.4.0
matz authored
899 rb_define_method(rb_cStruct, "initialize", rb_struct_initialize, -2);
6e52d10 * object.c (init_copy): rename copy_object as initialize_copy,
matz authored
900 rb_define_method(rb_cStruct, "initialize_copy", rb_struct_init_copy, 1);
3db12e8 Initial revision
matz authored
901
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
902 rb_define_method(rb_cStruct, "==", rb_struct_equal, 1);
6987b08 * struct.c (rb_struct_eql): should compare values with "eql?".
matz authored
903 rb_define_method(rb_cStruct, "eql?", rb_struct_eql, 1);
df61e2a @nobu * struct.c (rb_struct_hash): new methods Struct#hash, Struct#eql?.
nobu authored
904 rb_define_method(rb_cStruct, "hash", rb_struct_hash, 0);
3db12e8 Initial revision
matz authored
905
9d22a06 * array.c (rb_values_at): extract common procedure from
matz authored
906 rb_define_method(rb_cStruct, "to_s", rb_struct_inspect, 0);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
907 rb_define_method(rb_cStruct, "inspect", rb_struct_inspect, 0);
908 rb_define_method(rb_cStruct, "to_a", rb_struct_to_a, 0);
909 rb_define_method(rb_cStruct, "values", rb_struct_to_a, 0);
c18d374 991207
matz authored
910 rb_define_method(rb_cStruct, "size", rb_struct_size, 0);
911 rb_define_method(rb_cStruct, "length", rb_struct_size, 0);
3db12e8 Initial revision
matz authored
912
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
913 rb_define_method(rb_cStruct, "each", rb_struct_each, 0);
19c42c0 * variable.c (rb_obj_remove_instance_variable): raise NameError if
matz authored
914 rb_define_method(rb_cStruct, "each_pair", rb_struct_each_pair, 0);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
915 rb_define_method(rb_cStruct, "[]", rb_struct_aref, 1);
916 rb_define_method(rb_cStruct, "[]=", rb_struct_aset, 2);
a59c599 * string.c (rb_str_match_m): should convert an argument into
matz authored
917 rb_define_method(rb_cStruct, "select", rb_struct_select, -1);
f595d5b * array.c (rb_ary_values_at): new method to replace select(index..).
matz authored
918 rb_define_method(rb_cStruct, "values_at", rb_struct_values_at, -1);
3db12e8 Initial revision
matz authored
919
0bedb2e @nobu * intern.h, struct.c (rb_struct_s_members, rb_struct_members): public
nobu authored
920 rb_define_method(rb_cStruct, "members", rb_struct_members_m, 0);
3db12e8 Initial revision
matz authored
921 }
Something went wrong with that request. Please try again.