create array with iterative assignment

Viewed 163

Would like to exploit the following behaviour in Ruby

ary = Array.new(5) { |i|
   [i, j=2*i, k=j+1]
}

p ary #> [[0, 0, 1], [1, 2, 3], [2, 4, 5], [3, 6, 7], [4, 8, 9]]

It works for my purposes, but I couldn't find in the language definition whether this is legal Ruby. Is it? Or is it likely to break in the future?

[Edit] A smaller working example raising the same issue is

i = 1
ary = [i, j=2*i, k=j+1]

p ary #> [1, 2, 3]

But of course this example only has theoretical relevance contrary to the first, which does have practical relevance.

0 Answers
Related