title: Accessing an array variable in Ruby
body: <p>I'm currently teaching myself Ruby, via the 'Learn Ruby the Hard Way' course, and am having a problem referring to an array variable in a function. </p>
  
  <p>On the section on while loops, it is required to turn a while-loop into a function, adjusting the while-loop parameters to be variables. My code:  </p>
  
  <pre><code>#original while loop 
  while i &lt; 6 
    puts "At the top i is #{i}"
    numbers.push(i)
    i += 1
    puts "Numbers now: ", numbers
    puts "At the bottom i is #{i}"
  end
  
  #new function
  number_array = [] 
  def number_cruncher(number, cap)
    if number &lt; cap
      puts "At the top number is #{number}"
      number_array.push(number)
      number += 1
      puts "Numbers now: ", number_array
      puts "At the bottom number is #{number}"
      number_cruncher(number, cap, number_array)
    end
  end
  
  number_cruncher(0,6)
  </code></pre>
  
  <p>Running this code gives a NameError, because number_array as cited in the function is an undefined local variable. I can understand that this is wrong, but I can't work out what variable scope or syntax would allow me to refer to a variable whose starting point is an empty array - except by specifying a variable 'number_array' as a parameter of the function, and then declaring number_array = [], which is functional, but seems overcomplicated. </p>
  
  <p>I have researched variable scope in Ruby (here and elsewhere), but have nevertheless been unable to work this out.... Am 100% sure I am being really stupid, but help would nevertheless be appreciated!</p>