He is killed because you are entering an endless cycle. until a[b-1] will not end, because when you add nils to the array, you will get:
a == [1, 2, 3, nil, nil, nil, nil, nil, nil, nil]
after several iterations, and [b-1] will be nil , which is false. Until it stops.
About the second question, it is easy to extend the existing Array class:
class Array def padding(i, value=nil) (i - length).times { self << value } self end end
The result, as you expected:
[1,2,3].padding(10) #=> [1, 2, 3, nil, nil, nil, nil, nil, nil, nil] [1,2,3].padding(10, "YES") #=> [1, 2, 3, "YES", "YES", "YES", "YES", "YES", "YES", "YES"]
Note that the method modifies the existing array (therefore, due to Ruby conventions, you should call padding! ):
a = [1,2,3] #=> [1, 2, 3] a.padding(10, "YES") #=> [1, 2, 3, "YES", "YES", "YES", "YES", "YES", "YES", "YES"] a #=> [1, 2, 3, "YES", "YES", "YES", "YES", "YES", "YES", "YES"]
But of course, you can easily create a version of a method that does not change. I suggested that you want to modify the array because your original method did this.