Robert Klemme wrote:
> 2010/6/8 Juan Matias <jmrepetti / gmail.com>:
>>>
>> ?[1,2].combine([3,4]).combine([5,6,7])
>> ? ?end
>> ? ?aux.map {|elem| elem.flatten }
>> ?end
>> end
> 
> I'd rather do this:
> 
> module Enumerable
>   def combine(enum)
>     if block_given?
>       each do |*a|
>         enum.each do |*b|
>           yield *a, *b
>         end
>       end
>       self
>     else
>       enum_for(:combine, enum)
>     end
>   end
> end
> 
> [1,2].combine([3,4]) do |*a|
>   p a
> end
> 
> puts "--------------"
> 
> [1,2].combine([3,4]).each do |*a|
>   p a
> end
> 
> puts "--------------"
> 
> [1,2].combine([3,4]).combine([5,6]) do |*a|
>   p a
> end
> 
> puts "--------------"
> 
> [1,2].combine([3,4]).combine([5,6]).each do |*a|
>   p a
> end
> 
> Kind regards
> 
> robert

Great, I'll probe it,also I add a fix to my code:

class Array
  def combine(otherArray)
    aux = []
    return otherArray if self.empty? #this line
    self.each do |self_elem|
      otherArray.each do |other_elem|
        aux << [self_elem,other_elem]
      end
    end
    aux.map {|elem| elem.flatten }
  end
end

Juan Matias
-- 
Posted via http://www.ruby-forum.com/.