Quantcast
Channel: Making a Ruby array string into an array of integers - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Making a Ruby array string into an array of integers

$
0
0

I have an array (which is technically a string) of id numbers.

ids = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"

I want to make the ids into an array that looks like this:

ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

The only way I've found to do this is to use map.

id_numbers = ids.split(/,\s?/).map(&:to_i)

However, this lops off the first number in the array and replaces it with 0.

id_numbers = [0, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Is there a better way to go about converting a string array into a regular array?


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images