Skip to content

Commit 82731d9

Browse files
clayton-shopifybouk
authored andcommitted
Remove constant when a struct is redefined.
1 parent a630c4f commit 82731d9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Diff for: mrbgems/mruby-struct/src/struct.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * k
203203
}
204204
if (mrb_const_defined_at(mrb, mrb_obj_value(klass), id)) {
205205
mrb_warn(mrb, "redefining constant Struct::%S", name);
206-
/* ?rb_mod_remove_const(klass, mrb_sym2name(mrb, id)); */
206+
mrb_const_remove(mrb, mrb_obj_value(klass), id);
207207
}
208208
c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass);
209209
}

Diff for: mrbgems/mruby-struct/test/struct.rb

+21
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,24 @@
158158
assert_equal 1, a.dig(:purple, :red)
159159
assert_equal 1, a.dig(1, 0)
160160
end
161+
162+
assert("Struct.new removes existing constant") do
163+
begin
164+
assert_not_equal Struct.new("Test", :a), Struct.new("Test", :a, :b)
165+
ensure
166+
Struct.remove_const :Test
167+
end
168+
end
169+
170+
assert("Struct#initialize_copy requires struct to be the same type") do
171+
begin
172+
Struct.new("Test", :a)
173+
a = Struct::Test.new("a")
174+
Struct.new("Test", :a, :b)
175+
assert_raise(TypeError) do
176+
a.initialize_copy(Struct::Test.new("a", "b"))
177+
end
178+
ensure
179+
Struct.remove_const :Test
180+
end
181+
end

0 commit comments

Comments
 (0)