Alex Treber wrote:> Is there a different way to nest multiple loops?
Not built into the Ruby language as far as I know, but there''s nothing
stopping you from rolling your own code if you need to do a lot of this:
array1 = [ 1, 2, 3 ]
array2 = [ 4, 5, 6 ]
array3 = [ 7, 8, 9 ]
def iterate_over(*args, &block)
def _over(memo, rest, block)
if rest.empty?
block.call(memo)
else
(head, *tail) = rest
head.each do |x|
_over(memo + [x], tail, block)
end
end
end
_over([], args, block)
end
iterate_over(array1, array2, array3) do |x, y, z|
puts("#{x} #{y} #{z}")
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---