1

Say I have the following hash.

my_hash = {
  'array1' => %w[
    value1
    value2
  ],
  'array2' => %w[
    value3
    value4
  ]
}

How do I make an array that looks like

my_array = %w[value1 value2 value3 valuu4]
1
  • 1
    my_hash.values.flatten Commented Nov 10, 2015 at 15:37

2 Answers 2

3
my_array = my_hash.values.flatten
=> ["value1", "value2", "value3", "value4"]
Sign up to request clarification or add additional context in comments.

Comments

1

Flatten Hash Values

Use Hash#values to collect the values from your Hash, and then use Array#flatten to turn the result into a single Array rather than one containing nested arrays. For example:

my_hash.values.flatten
#=> ["value1", "value2", "value3", "value4"]

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.