Svarog
Svarog

Reputation: 2218

Ruby/Rhomobile: parse directory string representation


I have a string that looks like this: {"whatever-field"=>"gghyduudud"}
I'd like to parse it so that it becomes a hash.

Please help.

Thanks!

Upvotes: 0

Views: 187

Answers (2)

tokland
tokland

Reputation: 67900

You can use eval, but only if the data source is absolutely reliable:

>> eval('{"whatever-field"=>"gghyduudud"}')
=> {"whatever-field"=>"gghyduudud"} 

Upvotes: 1

Linuxios
Linuxios

Reputation: 35788

Here is a solution:

dictionary=Hash[*(dict_str[1..dict_str.length-2].split("=>").map {|strval| strval[1..strval.length-2]})]

That will work as long as you want the keys and values as strings. Its a bit long, but it worked for me.

Upvotes: 0

Related Questions