1

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]]

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

There's OrderedHash to preserve order.
There's also sort, which may change the order, but give a consistent output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.