Every combination of elements in two Ruby arrays

HKBR1907

I have three Ruby arrays:

color = ['blue', 'green', 'yellow']
names = ['jack', 'jill']
combination = []

I need the following concatenation inserted into the combination array:

FOR EACH names value: [name value] + " wants " + [color value]

So the outcome will be:

combination = ['jack wants blue','jack wants green','jack wants yellow','jill wants blue','jill wants green','jill wants yellow']

I can't figure out how to do this. I've tried this to start off with but no avail:

name.each do |name| 
   puts "#{name} wants #{color}"
end
Stefan

You can use Array#product:

names = ['jack', 'jill']
colors = ['blue', 'green', 'yellow']

names.product(colors).map { |name, color| "#{name} wants #{color}" }
#=> ["jack wants blue", "jack wants green", "jack wants yellow", "jill wants blue", "jill wants green", "jill wants yellow"]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Iterate every combination of two elements in HashSet

From Dev

Combination of elements of multiple arrays

From Dev

Get every possible combination of elements

From Dev

How do I create every combination of single elements selected from multiple arrays?

From Dev

Combination of elements from multiple arrays

From Dev

Merging only respective elements of two arrays in one array in ruby

From Dev

Every combination of list elements without replacement

From Dev

Applying a function to every combination of elements in a vector

From Dev

Applying a function to every combination of elements in a vector

From Dev

Editing script to account for every combination of two lists

From Dev

Getting every combination of two Pandas DataFrames?

From Dev

Find most common combination of elements in several arrays

From Dev

Find most common combination of elements in several arrays

From Dev

Compare two arrays in different columns in a file and print matching elements for every row using unix

From Dev

XSD that allows any combination of two elements

From Dev

Get all combination of two elements with distance 1

From Dev

Algorithm to generate every combination of n elements split into k sets

From Dev

Algorithm to generate every combination of n elements split into k sets

From Dev

Iterating through every combination of elements with repetitions without generating whole set

From Dev

Every two elements mean between these two element

From Dev

Merge two arrays of objects in Ruby

From Dev

Sort or map two arrays in ruby

From Dev

Comparing two arrays Ruby and sort

From Dev

Unequally interleave two ruby arrays

From Dev

How to get every permutation of elements in an array of arrays

From Dev

Ruby: Efficient way to compare two arrays of arrays

From Dev

Finding common elements in two arrays

From Dev

LotusScript Common elements in two Arrays

From Dev

Compare elements of two arrays of Object

Related Related

  1. 1

    Iterate every combination of two elements in HashSet

  2. 2

    Combination of elements of multiple arrays

  3. 3

    Get every possible combination of elements

  4. 4

    How do I create every combination of single elements selected from multiple arrays?

  5. 5

    Combination of elements from multiple arrays

  6. 6

    Merging only respective elements of two arrays in one array in ruby

  7. 7

    Every combination of list elements without replacement

  8. 8

    Applying a function to every combination of elements in a vector

  9. 9

    Applying a function to every combination of elements in a vector

  10. 10

    Editing script to account for every combination of two lists

  11. 11

    Getting every combination of two Pandas DataFrames?

  12. 12

    Find most common combination of elements in several arrays

  13. 13

    Find most common combination of elements in several arrays

  14. 14

    Compare two arrays in different columns in a file and print matching elements for every row using unix

  15. 15

    XSD that allows any combination of two elements

  16. 16

    Get all combination of two elements with distance 1

  17. 17

    Algorithm to generate every combination of n elements split into k sets

  18. 18

    Algorithm to generate every combination of n elements split into k sets

  19. 19

    Iterating through every combination of elements with repetitions without generating whole set

  20. 20

    Every two elements mean between these two element

  21. 21

    Merge two arrays of objects in Ruby

  22. 22

    Sort or map two arrays in ruby

  23. 23

    Comparing two arrays Ruby and sort

  24. 24

    Unequally interleave two ruby arrays

  25. 25

    How to get every permutation of elements in an array of arrays

  26. 26

    Ruby: Efficient way to compare two arrays of arrays

  27. 27

    Finding common elements in two arrays

  28. 28

    LotusScript Common elements in two Arrays

  29. 29

    Compare elements of two arrays of Object

HotTag

Archive