From: "Simon Kröger" Date: 2005-09-03T07:26:31+09:00 Subject: Re: Idiomatic conversion of yielding block to array Simon Kr�ger wrote: > You don't need the to_a but still zip will create 3 arrays. this does not, but its still not very ruby like: ------------------------------------------------------------- s1 = ['this', 'is', 'a', 'test'].join("\n") s2 = ['this', 'is', 'the', 'test'].join("\n") q1, q2 = SizedQueue.new(1), SizedQueue.new(1) Thread.new {s1.each_line {|l| q1 << l}; q1 << nil} Thread.new {s2.each_line {|l| q2 << l}; q2 << nil} line_no = 0 while true line_no += 1 t1, t2 = q1.pop, q2.pop break if !t1 || !t2 puts "difference on line #{line_no}" if t1 != t2 end ------------------------------------------------------------- perhaps better, but still kind of hardcore: ------------------------------------------------------------- require 'enumerator' require 'thread' module Enumerable def each_zip(other) q = SizedQueue.new(1) Thread.new {other.each{|l| q << l}} self.each{|a| yield a, q.pop} end end s1 = ['this', 'is', 'a', 'test'].join("\n") s2 = ['this', 'is', 'the', 'test'].join("\n") s1.to_enum(:each_line).enum_for(:each_zip, s2.to_enum(:each_line)).each_with_index do |t, l| puts "difference on line: #{l+1}" if t[0] != t[1] end ------------------------------------------------------------- food for thoughts... Simon