From: zoranlazarevic@... (Zoran Lazarevic) Date: 2003-10-07T08:23:13+09:00 Subject: map/collect iterating over multiple arrays/arguments Can I iterate over multiple arrays/collections? It is very useful (and used in Lisp a lot) to map several lists onto one, like map/collect but iterating over multiple collections at the same time. For example: foods = ['banana', 'grass', 'peanuts'] animals = ['monkey', 'gnu', 'elephant'] [animals,foods].multi_each{|x,y| puts "#{x} eats #{y}" } money eats banana gnu eats grass elephant eats peanuts [animals,foods].multi_map{|x,y| "#{x} eats #{y}" } => ["money eats banana", "gnu eats grass", "elephant eats peanuts"] This is relatively simple to implement using current Ruby features, but at the cost of either zipping all input collections in memory, or requiring that array implements integer indexing: foods.zip(animals).each{|x,y| puts "#{x} eats #{y}" } or module Enumerable def multi_map result = [] self[0].each_index{|i| args = self.collect{|x| x[i]} result << yield(*args) } result end end I assume this can also be done by using continuation, but is there a simpler way. Are there Iterators for enumerables, like in Java? --Laza Zoran Lazarevic 212.569.4011 http://www.cs.columbia.edu/~laza