On 01 Jun 2005, at 21:05, Nicholas Seckar wrote:

> ES wrote:
>
>> It may not be.
>>
>> left = [1, 2, 3, 4]
>> right = %w[one two three four]
>>
>> left.zip(right) {|l,r| puts "#{l} = #{r}"}
>>
>>
> That said, it can be tricky to implement zip in Ruby, and I'm  
> guessing the use of continuations is required.

Array#zip is built-in, but its not hard to implement.

 From MetaRuby (so it is written in a funky style and could be much  
shorter):

class Array
   def zip(*args)
     raise "I don't do that yet" if block_given?

     args = args.map { |a| convert a }
     args_len = args.length

     len = self.length
     result = Array.new len

     0.upto(length - 1) do |i|
       tmp = Array.new args_len + 1
       tmp[0] = self.at(i)

       0.upto(args_len - 1) do |j|
         tmp[j + 1] = args[j][i]
       end

       # I think you could make it support blocks here with yield(*tmp)
       result[i] = tmp
     end

     return result
   end

   private

   def convert(object)
     unless object.respond_to? :to_ary then
       raise TypeError, "cannot convert " + object.class.name + "  
into Array"
     end
     return object.to_ary
   end

end

(It would support blocks if I had tests for them, but they are  
missing from rubicon.)

-- 
Eric Hodel - drbrain / segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E  7C11 332A 551C 796C 9F04