One of the call sequences for method Array.new takes a block, allowing you to populate the new array with anything you want:
Array.new(size) {|index| ... }
This returns an array of the given size
; the block is called with each successive integer index
; the block's return value becomes the element for that index:
a = Array.new(3) {|index| "Element #{index}" }
a # => ["Element 0", "Element 1", "Element 2"]
More methods at #rubymethodoftheday.
Top comments (0)