ruby:如何将哈希转换为数组

前端之家收集整理的这篇文章主要介绍了ruby:如何将哈希转换为数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个包含数字的哈希:
{0=>0.07394653730860076,1=>0.0739598476853163,2=>0.07398647083461522}

它需要转换为如下数组:

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

我尝试了我的hash.values让我:

[0.07398921877505593,0.07400253683443543,0.07402917535044515]

我尝试了多种方法,但我刚开始学习ruby.

解决方法

试试这个:
{0=>0.07394653730860076,2=>0.07398647083461522}.to_a
#=> [[0,0.07398647083461522]]

猜你在找的Ruby相关文章