1

I have the following array

[["convertible", "2010", "red"], ["convertible", "2010", "green"]]

How do I merge the above array into this, in either in Rails or in Ruby?

["convertible", "2010", "red", "convertible", "2010", "green"]

Edit-1

@category.each do |content|
      form_chain = JSON.parse(content.content)
      chained_array << form_chain.values
    end

    chained_array

This gives the output

[["convertible", "2010", "red"], ["convertible", "2010", "green"]]

If I use chained_array.flatten! it gives the same result.

1
  • 1
    When in doubt, check the docs. Commented Oct 30, 2013 at 17:09

2 Answers 2

3
[["convertible", "2010", "red"], ["convertible", "2010", "green"]].flatten!
Sign up to request clarification or add additional context in comments.

2 Comments

It is working in my console but why it is not working in my rails application
got it some cache problem i cleared rake tmp:clear now working
0

Based on your edit you could just create a flat array from the beginning:

@category.each do |content|
      form_chain = JSON.parse(content.content)
      chained_array.push(*form_chain.values)
end

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.