Permalink
Browse files

Fix NameError on defining a struct with a name

Fix `NameError` on defining a struct with a name when a constant with
the same name already exists in the ancestors, e.g. in `Object` (global
space).
  • Loading branch information...
Slike9 committed Nov 6, 2018
1 parent 6b7ec84 commit 7833d9cb557236abee58532c18c2dbec62ad741e
Showing with 10 additions and 1 deletion.
  1. +1 −1 lib/concurrent/synchronization/abstract_struct.rb
  2. +9 −0 spec/concurrent/struct_shared.rb
@@ -138,7 +138,7 @@ def ns_initialize(*values)
end
unless name.nil?
begin
parent.send :remove_const, name if parent.const_defined? name
parent.send :remove_const, name if parent.const_defined?(name, false)
parent.const_set(name, clazz)
clazz
rescue NameError
@@ -10,6 +10,15 @@
expect(clazz.ancestors).to include described_class
end
it 'registers the class when given a class name which is defined in the ancestors' do
class_name = 'ValidClassName2'
Object.const_set(class_name, class_name)
clazz = described_class.new(class_name)
expect{ described_class.const_get(class_name) }.to_not raise_error
expect(clazz).to be_a Class
expect(clazz.ancestors).to include described_class
end
it 'creates an anonymous class when given at least one member' do
clazz = described_class.new(:foo)
expect{ described_class.const_get(clazz.to_s) }.to raise_error(NameError)

0 comments on commit 7833d9c

Please sign in to comment.