Forum: Ruby directly modify a part on an array for quiz #117 purposes

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
0a3117ace9ffd686054dfec667e9a976?d=identicon&s=25 Albert Ng (Guest)
on 2007-03-11 20:25
(Received via mailing list)
From Ruby quiz #117 (much fun)

#bad code by me, interesting bits shamelessly stolen from Dave Burt
neighborhood=[@grid[x0][y0], @grid[x0][y1], @grid[x1][y0],

where each element of @grid[][] is a string, x1 = x0+1, and y1=y0+1

is there a way to do the following:
@grid[x0][y0], @grid[x0][y1], @grid[x1][y0], @grid[x1][y1] =
@grid[x0][y0], @grid[x1][y1], @grid[x0][y1]

in a more readable way

I thought

*neighborhood = @grid[x1][y0], @grid[x0][y0], @grid[x1][y1],

would be equivalent, but it isn't.
E4d8c6e0e9e5ca6ab51b4b1937586e6f?d=identicon&s=25 Christoffer Lernö (Guest)
on 2007-03-11 20:35
(Received via mailing list)
On Mar 11, 2007, at 20:24 , Albert Ng wrote:

> [x1][y0],
> @grid[x0][y0], @grid[x1][y1], @grid[x0][y1]
> in a more readable way
> I thought
> *neighborhood = @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}=(v); @grid[@x + #{x}, @y + #{y}] = v;

To allow me to write the rotations as

     def ccw90
       self.xy00, self.xy10, self.xy01, self.xy11 = xy10, xy11, xy00,

     def cw90
       self.xy00, self.xy10, self.xy01, self.xy11 = xy01, xy00, xy11,

Might be an idea?

0a3117ace9ffd686054dfec667e9a976?d=identicon&s=25 Albert Ng (Guest)
on 2007-03-12 01:38
(Received via mailing list)
that's neat, I'll keep it in mind   :)
This topic is locked and can not be replied to.