Commit 1e892c0f authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #3280 from bouk/struct-redefine

Remove constant when a struct is redefined.
parents b4d50175 82731d9e
......@@ -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);
}
......
......@@ -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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment