Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of Numbers and Half Numbers in Ruby

Tags:

ruby

I'm trying to populate an array with sizes that are measured in whole and half numbers (i.e. 10, 10.5, 11, 11.5, 12). So far I have:

(10..12).map{ |size| [size, size + 0.5] }.flatten[0...-1]

Does a more eloquent way of doing this exist in Ruby without having to flatten and remove the last element?

like image 243
Stussa Avatar asked Jun 23 '15 19:06

Stussa


1 Answers

My personal favorite:

>> (10..12).step(0.5).to_a
=> [10.0, 10.5, 11.0, 11.5, 12.0]
like image 132
user12341234 Avatar answered Nov 09 '22 17:11

user12341234