summaryrefslogtreecommitdiff
path: root/struct.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-04-17 03:53:53 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-04-17 03:53:53 +0000
commit693efbb71035dede6459e0e5b73a5acdf38e5d90 (patch)
tree06d490c133a6197b62da77c11daedc98350a3589 /struct.c
parentf4c42434297a7b5a4a49c3d65d1a1d6885a8081f (diff)
struct_aref by member name
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@173 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'struct.c')
-rw-r--r--struct.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/struct.c b/struct.c
index 9f42d0aa9e..df35714adf 100644
--- a/struct.c
+++ b/struct.c
@@ -382,11 +382,39 @@ struct_aref(s, idx)
}
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)