Skip to content

Instantly share code, notes, and snippets.

@cronin101
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save cronin101/9312791 to your computer and use it in GitHub Desktop.

Select an option

Save cronin101/9312791 to your computer and use it in GitHub Desktop.
Array#Product
people = %i{Jacob James Maithu}
# => [:Jacob, :James, :Maithu]
bike_colours = %i{Red Black White}
# => [:Red, :Black, :White]
helmet_choices = [true, false]
# => [true, false]
people.product(bike_colours, helmet_choices).map do |p, c, h|
"#{p} riding a #{c} bike, #{h ? 'with' : 'without'} a helmet."
end
# => ["Jacob riding a Red bike, with a helmet.",
# "Jacob riding a Red bike, without a helmet.",
# "Jacob riding a Black bike, with a helmet.",
# "Jacob riding a Black bike, without a helmet.",
# "Jacob riding a White bike, with a helmet.",
# "Jacob riding a White bike, without a helmet.",
# "James riding a Red bike, with a helmet.",
# "James riding a Red bike, without a helmet.",
# "James riding a Black bike, with a helmet.",
# "James riding a Black bike, without a helmet.",
# "James riding a White bike, with a helmet.",
# "James riding a White bike, without a helmet.",
# "Maithu riding a Red bike, with a helmet.",
# "Maithu riding a Red bike, without a helmet.",
# "Maithu riding a Black bike, with a helmet.",
# "Maithu riding a Black bike, without a helmet.",
# "Maithu riding a White bike, with a helmet.",
# "Maithu riding a White bike, without a helmet."]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment