On Sat, 3 Feb 2001, Jim Freeze wrote:

> On Sat, 3 Feb 2001, David Alan Black wrote:
> 
> > #map is available, though not having each does seem asymmetrical
> > (there's #each2, #map, and #map2) and obviously #map is not exactly
> > the same thing as #each.
> 
> Would adding #each be as simple as adding the following to matrix.rb
> 
>   # ENUMERATIONS
>   def each
>     0.upto(size - 1) do
>       |i|
>       yield @elements[i]
>     end
>   end

That 0.upto(size - 1) idiom, as used in matrix.rb, keeps the two
vectors in sync in the '2' methods, but you wouldn't need it here.
Let Ruby do the work :-)

  def each(&block)
    @elements.each(&block)
    self
  end


(I threw in "self" for consistency with Enumerable#each.  Otherwise
you get @elements back.)

(Disclaimer: I'm approaching this abstractly as a Ruby puzzle -- there
may be reasons (mathematical or otherwise) that I'm unaware of for why
class Vector doesn't have #each.)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav