1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
This commit is contained in:
Benoit Daloze 2022-06-26 14:50:14 +02:00
parent f616e81637
commit d3d5ef0cca
74 changed files with 1201 additions and 324 deletions

View file

@ -300,6 +300,44 @@ describe 'Optional variable assignments' do
(@b[:k] ||= 12).should == 12
end
it 'correctly handles a splatted argument for the index' do
(@b[*[:k]] ||= 12).should == 12
end
it "evaluates the index precisely once" do
ary = [:x, :y]
@a[:x] = 15
@a[ary.pop] ||= 25
ary.should == [:x]
@a.should == { x: 15, y: 25 }
end
it "evaluates the index arguments in the correct order" do
ary = Class.new(Array) do
def [](x, y)
super(x + 3 * y)
end
def []=(x, y, value)
super(x + 3 * y, value)
end
end.new
ary[0, 0] = 1
ary[1, 0] = 1
ary[2, 0] = nil
ary[3, 0] = 1
ary[4, 0] = 1
ary[5, 0] = 1
ary[6, 0] = nil
foo = [0, 2]
ary[foo.pop, foo.pop] ||= 2
ary[2, 0].should == 2
ary[6, 0].should == nil
end
it 'returns the assigned value, not the result of the []= method with +=' do
@b[:k] = 17
(@b[:k] += 12).should == 29