code-gijoe
code-gijoe

Reputation: 7234

How to convert a serialized array string to an array?

I have this string :

temp = "["minutes", "hours"]"

If I do this:

temp[1..-2].split(", ")

I get an array of 2 elements like this:

[0] = ""minutes""
[1] = ""hours""

How can I avoid to have double quotes?

Upvotes: 1

Views: 829

Answers (3)

forker
forker

Reputation: 2679

One more:

the_string.scan(/\"(\w+)\"/).flatten
 => ["minutes", "hours"]

Upvotes: 2

Alfonso
Alfonso

Reputation: 759

Just do:

temp.gsub("\"", "")[1..-2].split(", ")

Or, once you have the array with double quotes on each element:

temp.map{|e| e.squeeze("̣\"")}

Upvotes: 1

Intrepidd
Intrepidd

Reputation: 20868

Use the JSON parser :

JSON.parse(your_array)

Upvotes: 4

Related Questions