Hi there! A newbie here who would appreciate any help you can give!
I’m doing a practice test in Chris P.'s intro to programming/Ruby
book. I’m creating a method to (recursively) sort the elements in a
given array.
def sortme unsortedarray
unsortedarray = unsortedarray.delete("")
testelement = unsortedarray.last
unsortedarray.each do |x|
if testelement < x
p = unsortedarray.index(x)
unsortedarray.delete(x)
unsortedarray[p] = “”
unsortedarray.push(x)
end
if testelement > x
end
end
sortme unsortedarray
return unsortedarray
end
givenarray = [“apples”,“pears”,“oranges”,“bananas”]
puts sortme givenarray
However when i run the program, i get this error:
filename.rb:3:in sortme': undefined method
last’ for nil:NilClass
(NoMethodError)
from filename.rb:21:in `’
Not sure what is going on with the “.last” function - any ideas?