From: Pit Capitain Date: 2005-08-17T08:35:19+09:00 Subject: Re: Array manipulations questions Hi Peter, I changed the arguments of the methods to start at 0: > a = > [ [10, 11, 20, 21], > [12, 13, 22, 23], > [30, 31, 40, 41], > [32, 33, 42, 43] ] def a.grid( i ) mid_row = size / 2 mid_col = self[ 0 ].size / 2 rows = if i < 2 then 0 ... mid_row else mid_row .. -1 end cols = if ( i % 2 ).zero? then 0 ... mid_col else mid_col .. -1 end self[ rows ].map { |elem| elem[ cols ] } end > a.grid(0) > ==> [10, 11], [12, 13] > a.grid(1) > ==> [20, 21], [22, 23] > a.grid(2) > ==> [30, 31], [32, 33] > a.grid(3) > ==> [40, 41], [42, 43] > > b = a.grid(0) > ==> [10, 11], [12, 13] > b[0][0] > ==> 10 > b[1][1] > ==> 13 def a.row( i ) self[ i ] end > a.row(0) > ==> [10, 11, 20, 21] def a.col( i ) map { |elem| elem[ i ] } end > a.col(0) > ==> [10, 12, 30, 32] Regards, Pit