Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4622

create array with iterative assignment

$
0
0

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 = 1ary = [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.


Viewing all articles
Browse latest Browse all 4622


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>