mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge pull request #3280 from bouk/struct-redefine
Remove constant when a struct is redefined.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user