diff options
-rw-r--r-- | struct.c | 27 | ||||
-rw-r--r-- | test/ruby/test_struct.rb | 4 |
2 files changed, 19 insertions, 12 deletions
@@ -148,16 +148,19 @@ rb_struct_modify(VALUE s) static VALUE rb_struct_set(VALUE obj, VALUE val) { - VALUE members, slot; + VALUE members, slot, fsym; long i, len; ID fid = rb_frame_this_func(); members = rb_struct_members(obj); len = RARRAY_LEN(members); rb_struct_modify(obj); + fid = rb_id_attrget(fid); + if (!fid) not_a_member(rb_frame_this_func()); + fsym = ID2SYM(fid); for (i=0; i<len; i++) { slot = RARRAY_AREF(members, i); - if (rb_id_attrset(SYM2ID(slot)) == fid) { + if (slot == fsym) { RSTRUCT_SET(obj, i, val); return val; } @@ -715,17 +718,17 @@ rb_struct_init_copy(VALUE copy, VALUE s) } static VALUE -rb_struct_aref_id(VALUE s, ID id) +rb_struct_aref_sym(VALUE s, VALUE name) { VALUE members = rb_struct_members(s); long i, len = RARRAY_LEN(members); for (i=0; i<len; i++) { - if (SYM2ID(RARRAY_AREF(members, i)) == id) { + if (RARRAY_AREF(members, i) == name) { return RSTRUCT_GET(s, i); } } - rb_name_error(id, "no member '%s' in struct", rb_id2name(id)); + rb_name_error_str(name, "no member '% "PRIsVALUE"' in struct", name); UNREACHABLE; } @@ -753,7 +756,7 @@ rb_struct_aref(VALUE s, VALUE idx) long i; if (RB_TYPE_P(idx, T_SYMBOL)) { - return rb_struct_aref_id(s, SYM2ID(idx)); + return rb_struct_aref_sym(s, idx); } else if (RB_TYPE_P(idx, T_STRING)) { ID id = rb_check_id_without_pindown(&idx); @@ -761,7 +764,7 @@ rb_struct_aref(VALUE s, VALUE idx) rb_name_error_str(idx, "no member '%"PRIsVALUE"' in struct", QUOTE(idx)); } - return rb_struct_aref_id(s, id); + return rb_struct_aref_sym(s, ID2SYM(id)); } i = NUM2LONG(idx); @@ -776,7 +779,7 @@ rb_struct_aref(VALUE s, VALUE idx) } static VALUE -rb_struct_aset_id(VALUE s, ID id, VALUE val) +rb_struct_aset_sym(VALUE s, VALUE name, VALUE val) { VALUE members = rb_struct_members(s); long i, len = RARRAY_LEN(members); @@ -787,13 +790,13 @@ rb_struct_aset_id(VALUE s, ID id, VALUE val) } for (i=0; i<len; i++) { - if (SYM2ID(RARRAY_AREF(members, i)) == id) { + if (RARRAY_AREF(members, i) == name) { rb_struct_modify(s); RSTRUCT_SET(s, i, val); return val; } } - rb_name_error(id, "no member '%s' in struct", rb_id2name(id)); + rb_name_error_str(name, "no member '% "PRIsVALUE"' in struct", name); UNREACHABLE; } @@ -823,7 +826,7 @@ rb_struct_aset(VALUE s, VALUE idx, VALUE val) long i; if (RB_TYPE_P(idx, T_SYMBOL)) { - return rb_struct_aset_id(s, SYM2ID(idx), val); + return rb_struct_aset_sym(s, idx, val); } if (RB_TYPE_P(idx, T_STRING)) { ID id = rb_check_id(&idx); @@ -831,7 +834,7 @@ rb_struct_aset(VALUE s, VALUE idx, VALUE val) rb_name_error_str(idx, "no member '%"PRIsVALUE"' in struct", QUOTE(idx)); } - return rb_struct_aset_id(s, id, val); + return rb_struct_aset_sym(s, ID2SYM(id), val); } i = NUM2LONG(idx); diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb index 2acec57808..37366c317e 100644 --- a/test/ruby/test_struct.rb +++ b/test/ruby/test_struct.rb @@ -158,6 +158,8 @@ module TestStruct assert_equal(1, o[0]) assert_raise(IndexError) { o[-2] } assert_raise(IndexError) { o[1] } + assert_raise_with_message(NameError, /foo/) {o["foo"]} + assert_raise_with_message(NameError, /foo/) {o[:foo]} end def test_aset @@ -167,6 +169,8 @@ module TestStruct assert_equal(2, o[:a]) assert_raise(IndexError) { o[-2] = 3 } assert_raise(IndexError) { o[1] = 3 } + assert_raise_with_message(NameError, /foo/) {o["foo"] = 3} + assert_raise_with_message(NameError, /foo/) {o[:foo] = 3} end def test_values_at |