How to convert a Range to an Array?

thought [0..8].to_a.map{|x| p! [x, x.class] } would do it, but, instead, this outputs:

# => [x, x.class] # => [0..8, Range(Int32, Int32)]

which tells me that .to_a doesn’t always generate an array. (It seems broken?)

I expected it to generate something like the following:

# => [x, x.class] # => [0, Int32]
# => [x, x.class] # => [1, Int32]
...
# => [x, x.class] # => [7, Int32]
# => [x, x.class] # => [8, Int32]

[0..8] isn’t a Range, its an Array(Range(Int32, Int32)) with a single element. to_a is just returning self. You probably want (0..8).to_a instead.

edit: :ninja: :smirk:

2 Likes

[0..8] is an Array(Range) already. You may want to do (0..8).map instead.

Edit: ninja’d

1 Like