Take the 2-minute tour ×

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?


share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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).

share|improve this answer
So simple, thank you. –  Ernest Oct 23 '10 at 0:04
There's OrderedHash to preserve order. –  hurikhan77 Oct 23 '10 at 0:11
There's also sort, which may change the order, but give a consistent output. –  Andrew Grimm Oct 24 '10 at 22:51
add comment

Your Answer


