0

我正在尝试编写一个 Ruby 递归函数,但我不断收到此错误。我的代码是这样的

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

出现以下错误消息,指向最后一条end语句:

语法错误,意外的 $end,预期为 keyword_end

这里的问题在哪里?

4

2 回答 2

1

根据古老的“授人以渔”谚语,我将回答更普遍的问题“我如何找到缺失或多余的东西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.

于 2012-10-21T07:22:47.650 回答
0

当我复制和粘贴您的代码时,我没有收到语法错误。您粘贴的函数周围是否还有其他代码可能缺少end

于 2012-10-21T07:22:25.423 回答