From: Albert Ng Date: 2007-03-12T09:37:41+09:00 Subject: Re: directly modify a part on an array for quiz #117 purposes ------=_Part_24107_21168241.1173659858439 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline that's neat, I'll keep it in mind :) On 3/11/07, Christoffer Lern=F6 wrote: > > On Mar 11, 2007, at 20:24 , Albert Ng wrote: > > > From Ruby quiz #117 (much fun) > > > > #bad code by me, interesting bits shamelessly stolen from Dave Burt > > neighborhood=3D[@grid[x0][y0], @grid[x0][y1], @grid[x1][y0], @grid[x1] > > [y1]] > > > > where each element of @grid[][] is a string, x1 =3D x0+1, and y1=3Dy0+1 > > > > is there a way to do the following: > > @grid[x0][y0], @grid[x0][y1], @grid[x1][y0], @grid[x1][y1] =3D @grid > > [x1][y0], > > @grid[x0][y0], @grid[x1][y1], @grid[x0][y1] > > > > in a more readable way > > > > I thought > > > > *neighborhood =3D @grid[x1][y0], @grid[x0][y0], @grid[x1][y1], @grid > > [x0][y1] > > > > would be equivalent, but it isn't. > > Did you have a look at my code? > > I used this: > > 2.times do |y| > 2.times do |x| > class_eval "def xy#{x}#{y}; @grid[@x + #{x}, @y + #{y}]; end" > class_eval "def xy#{x}#{y}=3D(v); @grid[@x + #{x}, @y + #{y}] =3D v; > end" > end > end > > To allow me to write the rotations as > > def ccw90 > self.xy00, self.xy10, self.xy01, self.xy11 =3D xy10, xy11, xy00, > xy01 > end > > def cw90 > self.xy00, self.xy10, self.xy01, self.xy11 =3D xy01, xy00, xy11, > xy10 > end > > > Might be an idea? > > > /Christoffer > > ------=_Part_24107_21168241.1173659858439--