Skip to content

Instantly share code, notes, and snippets.

@danielfone
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielfone/091cd5ac788ec446faa4 to your computer and use it in GitHub Desktop.
Save danielfone/091cd5ac788ec446faa4 to your computer and use it in GitHub Desktop.
Iterate over multiple arrays
x_coords = [...]
y_coords = [...]
x_coords.each do |x|
y_coords.each do |y|
do_point x, y
end
end
x_coords = [...]
y_coords = [...]
x_coords.product(y_coords).each do |x, y|
do_point x, y
end
# edit even better:
points = x_coords.product y_coords
points.each do |x, y|
do_point x, y
end
@eMBee
Copy link

eMBee commented Jun 17, 2014

i'd prefer the second. language features are there to be used.

and if this can be optimized then it is more likely that a builtin .product gets optimized than the parser detecting that a nested .each can be optimized.

greetings, eMBee

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment