Array manipulations questions

(Peter Fitzgibbons) #1

Hi. Thanks Why and 'captn.

If I wanted to include the array A as a class-accessor, how would I
extend the three def's to the variable?

IE:

class GridMaster
  attr_accessor :grid

  def create_grid
    @grid = [ ... ]
  end
End

gm = GridMaster.new
gm.create_grid
gm.grid.row(1)

???

Peter J. Fitzgibbons
Applications Manager
Lakewood Homes - "The American Dream Builder"(r)
Peter.Fitzgibbons@Lakewoodhomes.net
(847) 884-8800

···

-----Original Message-----
From: why the lucky stiff [mailto:ruby-talk@whytheluckystiff.net]
Sent: Tuesday, August 16, 2005 7:01 PM
To: ruby-talk ML
Subject: Re: Array manipulations questions

Let's start with the easy stuff.

Peter Fitzgibbons wrote:

A.row(1)
==> [10, 11, 20, 21]
  

a[0]
=> [10, 11, 20, 21]

def a.row i
  at i-1
end
a.row 1
=> [10, 11, 20, 21]

A.col(1)
==> [10, 12, 30, 32]

a.transpose[0]
=> [10, 12, 30, 32]

def a.col i
  transpose[i-1]
end
a.col 1
=> [10, 12, 30, 32]

As far as the `grid' method goes, do you want it to always do a 2x2
square or are you looking for some other math here?

  def a.grid i
    i -= 1
    (0..1).map do |j|
      at(((i/2)*2)+j)[(i%2)*2,2]
    end
  end

_why

(Brian Schröder) #2

Just extend Array with the functions

class Array
def row i
   at i-1
end

...
end

gm = GridMaster.new
gm.create_grid
gm.grid.row(1)

though I'm quite shure that this design is not very good. Maybe for a
start put create_grid into the initialize method

class GridMaster
  def initialize
    self.create_grid
  end
end

gm = GridMaster.new
gm.grid.row(1)

but I don't know what you want to achieve, so take this hint just as
an uninformed hint

regards,

Brian

···

On 17/08/05, Peter Fitzgibbons <Peter.Fitzgibbons@lakewoodhomes.net> wrote:

Hi. Thanks Why and 'captn.

If I wanted to include the array A as a class-accessor, how would I
extend the three def's to the variable?

IE:

class GridMaster
  attr_accessor :grid

  def create_grid
    @grid = [ ... ]
  end
End

>gm = GridMaster.new
>gm.create_grid
>gm.grid.row(1)
???

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/