Martin DeMello wrote:
also Array.new passes in the index to the block, so you can do
Array.new(4) {|i| i+1}
Here are some more alternatives including a solution with the famous
#inject.
a=Array.new(5) {|i| (i+1)%5}
=> [1, 2, 3, 4, 0]a=(1…4).to_a << 0
=> [1, 2, 3, 4, 0]a=(1…5).inject([]) {|ar,i| ar << (i%5)}
=> [1, 2, 3, 4, 0]a=(1…5).inject([]) {|ar,i| ar << i} << 0
=> [1, 2, 3, 4, 5, 0]
Have fun!
Kind regards
robert