开发者

Ruby: return a copy of array with [key, value] array from hash

In Python there is 开发者_运维知识库dict.inspect() method that returns a list of tuples (link). Is there a similar method in ruby to achieve, well, an array of arrays?

#input
{:a => 1, :b => 2}

#result
[[:a, 1], [:b, 2]]


Calling to_a on the hash will do that.

{:a => 1, :b => 2}.to_a
#=> [[:b, 2], [:a, 1]]

As you can see in the example output, the order is not necessarily preserved (at least not in ruby 1.8, in ruby 1.9 the order is preserved).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜