summaryrefslogtreecommitdiff
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
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
-rw-r--r--ChangeLog4
-rw-r--r--struct.c28
2 files changed, 32 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 58d0b9cb05c..82a5159da6d 100644
--- a/ChangeLog
+++ b/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.
diff --git a/struct.c b/struct.c
index 9f42d0aa9ed..df35714adf3 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)