Skip to content

Commit

Permalink
struct_aref by member name
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@173 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
matz committed Apr 17, 1998
1 parent f4c4243 commit 693efbb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
@@ -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.
Expand Down
28 changes: 28 additions & 0 deletions struct.c
Expand Up @@ -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)
Expand Down

0 comments on commit 693efbb

Please sign in to comment.