# Directly modify a part on an array for quiz #117 purposes

**URL:** <https://www.ruby-forum.com/t/directly-modify-a-part-on-an-array-for-quiz-117-purposes/88478>\
**Category:** Ruby\
**Created:** [March 11, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/directly-modify-a-part-on-an-array-for-quiz-117-purposes/88478 "2007-03-11T20:25:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert\_Ng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/839c29/32.png) [@Albert\_Ng](https://www.ruby-forum.com/u/Albert_Ng)\
**Post date:** [March 11, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/directly-modify-a-part-on-an-array-for-quiz-117-purposes/88478/1 "2007-03-11T20:25:44Z")

</div>

From Ruby quiz #117 (much fun)

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

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[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.

---

<div class="post-metadata">

**Author:** ![Albert\_Ng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/839c29/32.png) [@Albert\_Ng](https://www.ruby-forum.com/u/Albert_Ng)\
**Post date:** [March 11, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/directly-modify-a-part-on-an-array-for-quiz-117-purposes/88478/2 "2007-03-11T20:35:45Z")

</div>

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;  
end”  
end  
end

To allow me to write the rotations as

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

```

xy01  
end

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

```

xy10  
end

Might be an idea?

/Christoffer

---

<div class="post-metadata">

**Author:** ![Albert\_Ng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/839c29/32.png) [@Albert\_Ng](https://www.ruby-forum.com/u/Albert_Ng)\
**Post date:** [March 12, 2007, 1:38am UTC](https://www.ruby-forum.com/t/directly-modify-a-part-on-an-array-for-quiz-117-purposes/88478/3 "2007-03-12T01:38:19Z")

</div>

that’s neat, I’ll keep it in mind 🙂
