2

I have this array:

[[["a", "c"], "e"],
 [["a", "c"], "f"],
 [["a", "c"], "g"],
 [["a", "d"], "e"],
 [["a", "d"], "f"],
 [["a", "d"], "g"],
 [["b", "c"], "e"],
 [["b", "c"], "f"],
 [["b", "c"], "g"],
 [["b", "d"], "e"],
 [["b", "d"], "f"],
 [["b", "d"], "g"]]

I would like to turn it into this:

[["a", "c", "e"],
 ["a", "c", "f"],
 ["a", "c", "g"],
 ["a", "d", "e"],
 ["a", "d", "f"],
 ["a", "d", "g"],
 ["b", "c", "e"],
 ["b", "c", "f"],
 ["b", "c", "g"],
 ["b", "d", "e"],
 ["b", "d", "f"],
 ["b", "d", "g"]]

How can I do this with Ruby? I have looked at flatten by it seems to work from the outside in, not inside out.

4
  • 1
    You might want to look into why you're getting the array in the first form rather than the second. When I see data in the first form it's usually because I did something wrong and need to rethink my code. Commented Jun 24, 2013 at 16:08
  • @theTinMan, good point. I am getting the data in that form as a result of the product call. I'll post the call syntax to show how it was generated. Commented Jun 24, 2013 at 17:01
  • That works. Often we see questions dealing with a symptom as someone seeks a bandage, when the real problem needs a bit of refactoring or a different approach which ends up fixing a lot of other code. Commented Jun 24, 2013 at 18:35
  • @theTinMan, I created a separate question here: stackoverflow.com/questions/17286499/… Commented Jun 24, 2013 at 22:59

2 Answers 2

8

You could use flatten and map:

ar.map! {|i| i.flatten}
 # => [["a", "c", "e"],
 #     ["a", "c", "f"],
 #     ["a", "c", "g"],
 #     ["a", "d", "e"],
 #     ["a", "d", "f"],
 #     ["a", "d", "g"],
 #     ["b", "c", "e"],
 #     ["b", "c", "f"],
 #     ["b", "c", "g"],
 #     ["b", "d", "e"],
 #     ["b", "d", "f"],
 #     ["b", "d", "g"]]

Another one-liner would be :

 ar.map!(&:flatten)

 # => [["a", "c", "e"],
 #     ["a", "c", "f"],
 #     ["a", "c", "g"],
 #     ["a", "d", "e"],
 #     ["a", "d", "f"],
 #     ["a", "d", "g"],
 #     ["b", "c", "e"],
 #     ["b", "c", "f"],
 #     ["b", "c", "g"],
 #     ["b", "d", "e"],
 #     ["b", "d", "f"],
 #     ["b", "d", "g"]]
Sign up to request clarification or add additional context in comments.

3 Comments

Note &:flatten requires at least Ruby 1.9.3 or a similar version.
@JanDvorak Should I mention it in the post,that's why I have given two forms of answers.
Adding version information in the answer is always nice. "Since 1.x.x, you can also do \n..."
4

or try arr.each {|i| i.flatten!}

4 Comments

That depends on whether you want a destructive or a non-destructive version. OMG's answer is a non-destructive one.
@JanDvorak yes, but op said "I would like to turn it into this", so i think the original won't be needed
Thanks, I chose this one cause I don't need the original. I wish I could give half a check to both answers. Thanks everyone.
@JoshPetitt If you don’t need original then you can use also ar.map! {|i| i.flatten}. It will destroy the original one.

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.