Skip to content

Commit

Permalink
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions spec/ruby/language/optional_assignments_spec.rb
Original file line number Diff line number Diff line change
@@ -36,4 +36,41 @@
a.should == 10
end
end

describe 'using compunded constants' do
before do
Object.send(:remove_const, :A) if defined? Object::A
end

it 'with ||= assignments' do
Object::A ||= 10
Object::A.should == 10
end

it 'with ||= do not reassign' do
Object::A = 20
Object::A ||= 10
Object::A.should == 20
end

it 'with &&= assignments' do
Object::A = 20
Object::A &&= 10
Object::A.should == 10
end

it 'with &&= assignments will fail with non-existant constants' do
lambda { Object::A &&= 10 }.should raise_error(NameError)
end

it 'with operator assignments' do
Object::A = 20
Object::A += 10
Object::A.should == 30
end

it 'with operator assignments will fail with non-existant constants' do
lambda { Object::A += 10 }.should raise_error(NameError)
end
end
end

2 comments on commit 82d8c47

@kares
Copy link
Member

@kares kares commented on 82d8c47 Feb 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woow - totally did not know this was added to Ruby ... what an idea to get constants feel even less constant :)

Sorry, something went wrong.

@enebo
Copy link
Member Author

@enebo enebo commented on 82d8c47 Feb 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kares I felt the same way but 'A ||= 1' has always worked so I think they were just trying to be consistent.

Sorry, something went wrong.

Please sign in to comment.