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