mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
struct_aref by member name
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@173 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
f4c4243429
commit
693efbb710
2 changed files with 32 additions and 0 deletions
|
|
@ -1,3 +1,7 @@
|
|||
Fri Apr 17 11:06:30 1998 Yukihiro Matsumoto <matz@netlab.co.jp>
|
||||
|
||||
* struct.c (struct_aset): struct member can be set by member name.
|
||||
|
||||
Thu Apr 16 16:52:01 1998 Yukihiro Matsumoto <matz@netlab.co.jp>
|
||||
|
||||
* experimental release 1.1b9_11.
|
||||
|
|
|
|||
28
struct.c
28
struct.c
|
|
@ -381,12 +381,40 @@ struct_aref(s, idx)
|
|||
return RSTRUCT(s)->ptr[i];
|
||||
}
|
||||
|
||||
VALUE
|
||||
struct_aset_id(s, id, val)
|
||||
VALUE s, val;
|
||||
ID id;
|
||||
{
|
||||
VALUE member;
|
||||
int i, len;
|
||||
VALUE *p;
|
||||
|
||||
member = rb_iv_get(CLASS_OF(s), "__member__");
|
||||
if (NIL_P(member)) {
|
||||
Bug("non-initialized struct");
|
||||
}
|
||||
|
||||
len = RARRAY(member)->len;
|
||||
for (i=0; i<len; i++) {
|
||||
if (FIX2INT(RARRAY(member)->ptr[i]) == id) {
|
||||
RSTRUCT(s)->ptr[i] = val;
|
||||
return val;
|
||||
}
|
||||
}
|
||||
NameError("no member '%s' in struct", rb_id2name(id));
|
||||
}
|
||||
|
||||
VALUE
|
||||
struct_aset(s, idx, val)
|
||||
VALUE s, idx, val;
|
||||
{
|
||||
int i;
|
||||
|
||||
if (TYPE(idx) == T_STRING) {
|
||||
return struct_aref_id(s, rb_to_id(idx));
|
||||
}
|
||||
|
||||
i = NUM2INT(idx);
|
||||
if (i < 0) i = RSTRUCT(s)->len + i;
|
||||
if (i < 0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue