summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-struct
diff options
context:
space:
mode:
authorClayton Smith <[email protected]>2016-11-16 14:09:20 -0500
committerBouke van der Bijl <[email protected]>2016-11-24 10:04:09 -0500
commit82731d9ee0964b0131a9ae25e81dbec583d3af28 (patch)
treedbda2acc9279e46c80022d59658166b33507887c /mrbgems/mruby-struct
parenta630c4f413f6af764e68210430e8b61a435d38d7 (diff)
downloadmruby-82731d9ee0964b0131a9ae25e81dbec583d3af28.tar.gz
mruby-82731d9ee0964b0131a9ae25e81dbec583d3af28.zip
Remove constant when a struct is redefined.
Diffstat (limited to 'mrbgems/mruby-struct')
-rw-r--r--mrbgems/mruby-struct/src/struct.c2
-rw-r--r--mrbgems/mruby-struct/test/struct.rb21
2 files changed, 22 insertions, 1 deletions
diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c
index 0ccb7f4cb..8c8f54f27 100644
--- a/mrbgems/mruby-struct/src/struct.c
+++ b/mrbgems/mruby-struct/src/struct.c
@@ -203,7 +203,7 @@ make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * k
}
if (mrb_const_defined_at(mrb, mrb_obj_value(klass), id)) {
mrb_warn(mrb, "redefining constant Struct::%S", name);
- /* ?rb_mod_remove_const(klass, mrb_sym2name(mrb, id)); */
+ mrb_const_remove(mrb, mrb_obj_value(klass), id);
}
c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass);
}
diff --git a/mrbgems/mruby-struct/test/struct.rb b/mrbgems/mruby-struct/test/struct.rb
index 02ecf69e4..bbfe18cb2 100644
--- a/mrbgems/mruby-struct/test/struct.rb
+++ b/mrbgems/mruby-struct/test/struct.rb
@@ -158,3 +158,24 @@ assert("Struct#dig") do
assert_equal 1, a.dig(:purple, :red)
assert_equal 1, a.dig(1, 0)
end
+
+assert("Struct.new removes existing constant") do
+ begin
+ assert_not_equal Struct.new("Test", :a), Struct.new("Test", :a, :b)
+ ensure
+ Struct.remove_const :Test
+ end
+end
+
+assert("Struct#initialize_copy requires struct to be the same type") do
+ begin
+ Struct.new("Test", :a)
+ a = Struct::Test.new("a")
+ Struct.new("Test", :a, :b)
+ assert_raise(TypeError) do
+ a.initialize_copy(Struct::Test.new("a", "b"))
+ end
+ ensure
+ Struct.remove_const :Test
+ end
+end