I am trying to write a Ruby recursive function, but I am getting this error continuously. My code goes something like this
def myfun(mylist)
nextlist = []
if mylist.size == 1
return (mylist[0])
else
# populate the list "nextlist" with fewer elements as compared to mylist somehow
end
return myfun(nextlist)
end
The following error message comes up pointing at the last end statement:
syntax error, unexpected $end, expecting keyword_end
Where is the problem here?
According to the old “teach a man to fish” proverb, I’ll answer the more general question “how do I find a missing or superfluous
end?”.For this, I find it very helpful to use the auto-indenting feature of my editor. In vim, I just hit gg=G to indent the entire file, and scan through it visually to find where the indent starts being different from what I’d expect.