[Tioga-users] dvector.each_many ?

Edwin edder at tkwsping.nl
Thu Apr 12 05:08:42 EDT 2007


> I have loved the routine
>
> @x.each2(@y) do |x,y| ... end
>
> to progress into 2 dvectors simultaneously, but unfortunately, I do
> most of my work (when using dvectors outside of Tioga) with 3
> dimensions or more so I wonder if it would be possible to implement an
> each_many routine that could be used as
>
> @x.each_many(@y, at z, at t, at u, at v, at w) do |x,y,z,t,u,v,w| ... end
>
> and which determines automaticaly how many Dvectors to use from the
> number of arguments.
>
> For the moment, I get along with indexes but it's less convenient in  
> writing.


You can add this capability yourself, by augmenting the Dvector class as  
follows:

module Dobjects
   class Dvector
     def each_many( *vectors, &block )
        self.each_index do |i|
          args = Array.new
          vectors.each { |v| args.push( v[i] )  }
          block.call( self[i], *args )
       end
     end
   end
end

Example:

require 'Dobjects/Dvector'
include Dobjects
module Dobjects
   class Dvector
     def each_many( *vectors, &block )
        self.each_index do |i|
          args = Array.new
          vectors.each { |v| args.push( v[i] )  }
          block.call( self[i], *args )
       end
     end
   end
end

v1 = Dvector[1,2]
v2 = Dvector[3,4]
v3 = Dvector[5,6]
v1.each_many(v2,v3) { |x,y,z| p x+y+z }
=> 9.0
=> 12.0


More information about the Tioga-users mailing list