Quantcast
Viewing latest article 4
Browse Latest Browse All 4

Making a Ruby array string into an array of integers

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 latest article 4
Browse Latest Browse All 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>