[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:02090] Re: Array other wonder

From: Dave Thomas <Dave@...>
Date: 2000-03-23 00:08:49 UTC
List: ruby-talk #2090
"Dat Nguyen" <thucdat@hotmail.com> writes:

> According to the Ruby manual:
> self + other
> The array concatenation
> 
> This takes away the other natural operator addition or plus. There are 
> applications that need to add two numeric arrays with the same dimension, 
> no?
> 
> Would the solution be creating a subclass that introduces a new method like:
> self.add other
> 
> But where would the sum go?

Perhaps you could use the Matrix package - a vector is a Matrix with
only one row (or only one column, depending on how you look at it)

Alternatively, you could implement your own using something like:

     require 'delegate'

     class Vector < SimpleDelegator
       attr :value
       def initialize(anArray)
         super(anArray)
       end
       def +(aVector)
         raise "Size mismatch" if size != aVector.size
         i = -1
         collect { |me| me + aVector[i += 1] }
       end
     end

     v1 = Vector.new [ 1,2,3,4,5 ]
     v2 = Vector.new [ 10, 12, 14, 16, 18 ]

     p (v1 + v2)

Dave

ps. Question to the list: what's a better way of concisely
implementing the parallel iteration through the two arrays?


In This Thread

Prev Next