jissy
jissy

Reputation: 463

get the values of a hash as integer instead of string in rails

I have a hash getting in the controller from coffeescript:

h = {"1"=>"[2, 44]", "3"=>"[50]", "4"=>"[43, 42]", "9"=>"[48, 40, 45, 41]"}

Actually, I need the value as integers,but here it returns as a string like the below:

h["9"] , it will return  "[48, 40, 45, 41]" .

How can I get it as an integer array.Please help

Thanks

Upvotes: 3

Views: 557

Answers (1)

Sebastián Palma
Sebastián Palma

Reputation: 33420

You could parse each value as a JSON:

h.transform_values { |value| JSON.parse(value) }
# => {"1"=>[2, 44], "3"=>[50], "4"=>[43, 42], "9"=>[48, 40, 45, 41]}

Upvotes: 5

Related Questions