What is the shortest way to create this array in Ruby:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Thanks for any help!
What is the shortest way to create this array in Ruby:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Thanks for any help!
What about Range#step:
(10..100).step(10).to_a
#=> [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Or Numeric#step:
10.step(100, 10).to_a
#=> [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
You can use Range and call Enumerable#map method on it, like this:
(1..10).map{|i| i * 10}
# => [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Or, as suggested by @JörgWMittag, with Object#method method that returns Method instance which is converted to proc by & notation:
(1..10).map(&10.method(:*))
# => [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]