From: Peter Fitzgibbons Date: 2005-08-17T19:50:38+09:00 Subject: Re: Array manipulations questions 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 Irb>gm = GridMaster.new Irb>gm.create_grid Irb>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