0

I have something like this:

[#TrajectoryMeasurement depth: 0, move_e: 234>,
#TrajectoryMeasurement depth: 1475, move_e: 123>]

How to convert it to:

[[0, 234], [1475,123]]
5
  • 1
    This is not a Hash. This is an array of objects. Commented Dec 5, 2013 at 9:28
  • @quetzalcoatl It is neither. It is not a valid Ruby code. Commented Dec 5, 2013 at 9:29
  • @sawa: I'll not argue, I'm just guessing as the notation seems broken to me. Is it a normal dump of some construct? Commented Dec 5, 2013 at 9:31
  • @plewas, does TrajectoryMeasurement respond to methods depth and move_e? Commented Dec 5, 2013 at 9:31
  • @quetzalcoatl I checked the source of the OP's original posting. I don't think the mess is because of interaction with the markup. The OP simply posted invalid code. Commented Dec 5, 2013 at 9:43

3 Answers 3

1

If it's an array of objects as I suspect you can use the #collect method on Array:

array = [#TrajectoryMeasurement depth: 0, move_e: 234>,
#TrajectoryMeasurement depth: 1475, move_e: 123>]

array.collect { |x| [x.depth, x.move_e] }
# => [[0, 234], [1475, 123]]
Sign up to request clarification or add additional context in comments.

Comments

0

Supposing you really had a hash, all you need is calling .to_a to get exactly what you asked for.

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

Alas, as it was said before, it wouldn't see, what you have there is a Hash, unless that's an ad-hoc representation of it.

Comments

0

Depending on the structure of your hash you might also want to have a look at .flatten.

Comments

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.