title: Unsure why argument is being altered by while loop in Ruby
body: <p>Very (<em>very, very</em>) new to Ruby, so apologies for this awful code. </p>
  
  <p>I have the following 2d array;</p>
  
  <pre><code>test = [
    ['Z','M','Z','Z','Z'],
    ['Z','Z','M','M','Z'],
    ['Z','Z','M','Z','Z'],
    ['Z','Z','Z','Z','Z'],
    ['Z','M','Z','Z','M']
  ]
  </code></pre>
  
  <p>and I'd like to return the adjacent cells (up to 8) of every cell in the array. I'm trying this on a row-by-row basis, using 'while' to loop through each sub-array until I hit an index that's out of bounds. </p>
  
  <p>I'm sure this is horribly inefficient, dumb etc, but for now it's where my head is!</p>
  
  <p>When I call the method as per the below, passing the arguments (test, 0, 4), it correctly works it's way along the 'x' indices, but I am absolutely baffled as to why my 'y' argument gets changed from a '4' to a '1' after the first loop. </p>
  
  <p>What am I missing?</p>
  
  <pre><code>def do_the_cells_in_one_row grid,x=0,y=0
    while grid[y][x].nil? == false
      puts "here's x: #{x} y: #{y}"
      puts grid[y][x]
  
      if (x-1).negative? == false
        print grid[y][x-1] 
        print y
        puts x-1
      end
  
      if (x-1).negative? == false &amp;&amp; (y-1).negative? ==false
        print grid[y-1][x-1]
        print y-1
        puts x
      end
  
      if (y+1) &lt; grid.length &amp;&amp; (x-1).negative? == false
        print grid[y+1][x-1]
        print y+1 
        puts x-1
      end
  
      if (y+1) &lt; grid.length
        print grid[y+1][x]
        print y+1
        puts x
      end
  
      if (y-1).negative? == false
        print grid[y-1][x]
        print y-1
        puts x
      end
  
      if  x+1 &lt; grid[y].length
        print grid[y][x+1]
        print y
        puts x+1
      end
  
      if  y+1 &lt; grid.length &amp;&amp; x+1 &lt; grid[y].length
        print grid[y+1][x+1]
        print y+1
        puts x+1
      end
  
      if (y-1).negative? == false &amp;&amp; (x+1) &lt; grid[y].length
        print grid[y-1][x+1]
        print y-1
        puts x+1
      end
  
      x += 1
    end
  end
  
  do_the_cells_in_one_row(test,0,4)
  </code></pre>
  
  <p><strong>EDIT</strong> updated the code as per the comments! Running straight into another problem unfortunately. I'm trying this to then loop the code through each subarray;</p>
  
  <pre><code>def do_every_row grid,x=0,y=0
    while grid[y][x].nil? == false
      do_the_cells_in_one_row(grid,x,y)
      y += 1
    end
  end
  </code></pre>
  
  <p>get the following error </p>
  
  <pre><code>in `do_every_row': undefined method `[]' for nil:NilClass (NoMethodError)
  </code></pre>
  
  <p>but unsure why!</p>