Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby: how to convert hash into array

Tags:

arrays

ruby

hash

I have a hash that contains numbers as such:

{0=>0.07394653730860076, 1=>0.0739598476853163, 2=>0.07398647083461522}

it needs to be converted into an array like:

[[0, 0.07394653730860076], [1, 0.0739598476853163], [2, 0.07398647083461522]]

i tried my hash.values which gets me:

[0.07398921877505593, 0.07400253683443543, 0.07402917535044515]

I have tried multiple ways but i just started learning ruby.

like image 604
user2376068 Avatar asked May 14 '13 20:05

user2376068


1 Answers

try this:

{0=>0.07394653730860076, 1=>0.0739598476853163, 2=>0.07398647083461522}.to_a
#=> [[0, 0.07394653730860076], [1, 0.0739598476853163], [2, 0.07398647083461522]]
like image 134
Arup Rakshit Avatar answered Oct 18 '22 07:10

Arup Rakshit