views:

391

answers:

2

I have a string

"1,2,3,4"

and I'd like to convert it into an array:

[1,2,3,4]

How?

+4  A: 

"1,2,3,4".split(",") as strings

"1,2,3,4".split(",").map { |s| s.to_i } as integers

Oliver N.
+11  A: 
>> "1,2,3,4".split(",")
=> ["1", "2", "3", "4"]

Or for integers:

>> "1,2,3,4".split(",").map { |s| s.to_i }
=> [1, 2, 3, 4]
Shadwell
+1 i saw yours and realized i had to change each to map
Oliver N.
Remember, if you're using >=1.9, you can just use "1,2,3,4".split(',').map(:to_i)
Alex Fort
jonnii