On Mar 28, 2006, at 2:08 AM, Eric Luo wrote: > I want to make a Array multipled by another Array, and the > following semantic. > For example: > [1, 2, 3] * [4, 5] > then I will want [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]] > returned. a = [1, 2, 3] b = [4, 5] (a * b.size).zip(b * a.size).sort # => [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]] # or w/o sort: a.zip(a).flatten.zip(b * a.size) # => [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]] # the latter only works as-is because b.size == 2 (a.zip(a).flatten ~= a * 2). # If b.size were 3, you'd add b.size-1 a's to the first zip arg list: b << 6 a.zip(a, a).flatten.zip(b * a.size) # => [[1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6]] # if order doesn't actually matter, the first solution is the cleanest: class Array def x(b) a = self (a * b.size).zip(b * a.size) end end a.x b => [[1, 4], [2, 5], [3, 6], [1, 4], [2, 5], [3, 6], [1, 4], [2, 5], [3, 6]] -- ryand-ruby / zenspider.com http://www.zenspider.com/seattle.rb - Seattle.rb http://blog.zenspider.com/