diff options
author | Benoit Daloze <[email protected]> | 2019-09-29 18:01:32 +0200 |
---|---|---|
committer | Benoit Daloze <[email protected]> | 2019-09-29 18:01:32 +0200 |
commit | a17bc04d159ec9839cc8cfb02dc0cdd2802110f4 (patch) | |
tree | a10d4121aeb1517c198ab5b1577bca93912dc1f9 /spec/ruby/language/optional_assignments_spec.rb | |
parent | f9a9f3c7c6cf3fe534d4934dd3b502d721151b81 (diff) |
Update to ruby/spec@e69a14c
Diffstat (limited to 'spec/ruby/language/optional_assignments_spec.rb')
-rw-r--r-- | spec/ruby/language/optional_assignments_spec.rb | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/spec/ruby/language/optional_assignments_spec.rb b/spec/ruby/language/optional_assignments_spec.rb index 04abc2496b..3d9e0dbb65 100644 --- a/spec/ruby/language/optional_assignments_spec.rb +++ b/spec/ruby/language/optional_assignments_spec.rb @@ -42,6 +42,18 @@ describe 'Optional variable assignments' do a.should == 10 end + + it 'returns the new value if set to false' do + a = false + + (a ||= 20).should == 20 + end + + it 'returns the original value if truthy' do + a = 10 + + (a ||= 20).should == 10 + end end describe 'using a accessor' do @@ -89,6 +101,49 @@ describe 'Optional variable assignments' do @a.b.should == 10 end + + it 'returns the new value if set to false' do + def @a.b=(x) + :v + end + + @a.b = false + (@a.b ||= 20).should == 20 + end + + it 'returns the original value if truthy' do + def @a.b=(x) + @b = x + :v + end + + @a.b = 10 + (@a.b ||= 20).should == 10 + end + + it 'works when writer is private' do + klass = Class.new do + def t + self.b = false + (self.b ||= 10).should == 10 + (self.b ||= 20).should == 10 + end + + def b + @b + end + + def b=(x) + @b = x + :v + end + + private :b= + end + + klass.new.t + end + end end |