Skip to content

Commit dd6b5f5

Browse files
authored
Merge pull request #776 from Slike9/fix-struct-with-class-name-definition
Fix NameError on defining a struct with a name
2 parents 675c746 + 7833d9c commit dd6b5f5

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/concurrent/synchronization/abstract_struct.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def ns_initialize(*values)
138138
end
139139
unless name.nil?
140140
begin
141-
parent.send :remove_const, name if parent.const_defined? name
141+
parent.send :remove_const, name if parent.const_defined?(name, false)
142142
parent.const_set(name, clazz)
143143
clazz
144144
rescue NameError

spec/concurrent/struct_shared.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
expect(clazz.ancestors).to include described_class
1111
end
1212

13+
it 'registers the class when given a class name which is defined in the ancestors' do
14+
class_name = 'ValidClassName2'
15+
Object.const_set(class_name, class_name)
16+
clazz = described_class.new(class_name)
17+
expect{ described_class.const_get(class_name) }.to_not raise_error
18+
expect(clazz).to be_a Class
19+
expect(clazz.ancestors).to include described_class
20+
end
21+
1322
it 'creates an anonymous class when given at least one member' do
1423
clazz = described_class.new(:foo)
1524
expect{ described_class.const_get(clazz.to_s) }.to raise_error(NameError)

0 commit comments

Comments
 (0)