Learning Insertion Sort in Ruby
You are overruning the bounds of your array. The example you were given was assuming 1-indexed arrays, but arrays in ruby are 0-indexed. The first line should be
for j in 1...num.length
The other answer is correct that you are going past the end of the values in the array because it is 0-based but there are other changes you need to make to make the algorithm work:
for j in 1..(num.length - 1)
and
while i >= 0 and num[i] > key