Skip to content

Commit 3186b2f

Browse files
committed
Add spec for index argument evaluation order in optional assignment.
1 parent 67ce7af commit 3186b2f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

spec/ruby/language/optional_assignments_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,32 @@ def []=(k, v)
312312
@a.should == { x: 15, y: 25 }
313313
end
314314

315+
it "evaluates the index arguments in the correct order" do
316+
ary = Class.new(Array) do
317+
def [](x, y)
318+
super(x + 3 * y)
319+
end
320+
321+
def []=(x, y, value)
322+
super(x + 3 * y, value)
323+
end
324+
end.new
325+
ary[0, 0] = 1
326+
ary[1, 0] = 1
327+
ary[2, 0] = nil
328+
ary[3, 0] = 1
329+
ary[4, 0] = 1
330+
ary[5, 0] = 1
331+
ary[6, 0] = nil
332+
333+
foo = [0, 2]
334+
335+
ary[foo.pop, foo.pop] ||= 2
336+
337+
ary[2, 0].should == 2
338+
ary[6, 0].should == nil
339+
end
340+
315341
it 'returns the assigned value, not the result of the []= method with +=' do
316342
@b[:k] = 17
317343
(@b[:k] += 12).should == 29

0 commit comments

Comments
 (0)