title: Learning Insertion Sort in Ruby
body: <p>I have just started the MIT Introduction to Algorithms course through the material posted online. Along with the course I have also decided to learn/enhance my Ruby skills by coding the algorithms in it. </p>
  
  <p>I am on the first algorithm given, which is Insertion sort, and I have the following code typed up but I am getting this error when I run it:</p>
  
  <blockquote>
    <p>insertionsort.rb:5:in `>': comparison of Fixnum with nil failed (ArgumentError)</p>
  </blockquote>
  
  <pre><code>def  insertionsort(num)
  for j in 2..num.length
      key = num[j]
      i = j - 1
      while i &gt; 0 and num[i] &gt; key
          num[i+1] = num[i]
          i = i - 1
      end
      num[i+1] = key
  end 
  puts num
  end
  
  numbers = [23,34,46,87,12,1,66]
  
  insertionsort(numbers)
  </code></pre>
  
  <p>I'm sure it is a fairly basic problem but I just can't grasp what it is at the moment. Any  help or tips would be very much appreciated. </p>