Reputation: 4013
I have the following string
78,87,test,test1,125
How can i convert this into an array
I need something like this
[78, 87, "test", "test1", 125]
How do i achieve it.
Upvotes: 0
Views: 75
Reputation: 38645
Adding this answer just for completeness although @struthersneil's answer answers most of it.
"78,87,test,test1,125".split(',').map { |x| x=~ /^\d+$/ ? x.to_i : x }
> [78, 87, "test", "test1", 125]
Note the use of map
and regex
check for numbers. You should be able to change the regex and appropriate helpers e.g. to_i
, to_f
etc. according to your need.
Upvotes: 1
Reputation: 2750
You'll have an easier time with ruby if you familiarize yourself with the commonly-used classes, like String: http://ruby-doc.org/core-2.0.0/String.html.
What you want here is:
"something,something,something".split ','
Upvotes: 0