I initialize an array this way:
array = Array.new
array << \'1\' << \'2\' << \'3\'
Is it possible to do that in one s
To prove There's More Than One Six Ways To Do It:
plus_1 = 1.method(:+)
Array.new(3, &plus_1) # => [1, 2, 3]
If 1.method(:+) wasn't possible, you could also do
plus_1 = Proc.new {|n| n + 1}
Array.new(3, &plus_1) # => [1, 2, 3]
Sure, it's overkill in this scenario, but if plus_1
was a really long expression, you might want to put it on a separate line from the array creation.