summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--mrbgems/mruby-struct/src/struct.c10
-rw-r--r--mrbgems/mruby-struct/test/struct.rb3
2 files changed, 12 insertions, 1 deletions
diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c
index a15655dbb..6894ffec9 100644
--- a/mrbgems/mruby-struct/src/struct.c
+++ b/mrbgems/mruby-struct/src/struct.c
@@ -660,11 +660,19 @@ mrb_struct_aset(mrb_state *mrb, mrb_value s)
mrb_get_args(mrb, "oo", &idx, &val);
+ if (mrb_string_p(idx)) {
+ mrb_value sym = mrb_check_intern_str(mrb, idx);
+
+ if (mrb_nil_p(sym)) {
+ mrb_raisef(mrb, E_INDEX_ERROR, "no member '%S' in struct", idx);
+ }
+ idx = sym;
+ }
if (mrb_symbol_p(idx)) {
return mrb_struct_aset_sym(mrb, s, mrb_symbol(idx), val);
}
- i = mrb_fixnum(idx);
+ i = mrb_int(mrb, idx);
if (i < 0) i = RSTRUCT_LEN(s) + i;
if (i < 0) {
mrb_raisef(mrb, E_INDEX_ERROR,
diff --git a/mrbgems/mruby-struct/test/struct.rb b/mrbgems/mruby-struct/test/struct.rb
index b3b9ce33f..f4151c493 100644
--- a/mrbgems/mruby-struct/test/struct.rb
+++ b/mrbgems/mruby-struct/test/struct.rb
@@ -39,6 +39,9 @@ assert('Struct#[]=', '15.2.18.4.3') do
cc = c.new(1,2)
cc[:m1] = 3
cc[:m1] == 3
+ cc["m2"] = 3
+ assert_equal 3, cc["m2"]
+ assert_raise(TypeError) { cc[[]] = 3 }
end
assert('Struct#each', '15.2.18.4.4') do