#创建评论application.comments.create{:text=>"this is a nice app"}movie.comments.create{:text=>"this is a nice movie"}game.comments.create{:text=>"this is a nice game"}#查看评论application.commentsmovie.commentsgame.comments
classCommentbelongs_to:commentable,:polymorphic=>trueendclassApplicationhas_many:comments,:as=>:commentableendclassMoviehas_many:comments,:as=>:commentableendclassGamehas_many:comments,:as=>:commentableend#添加评论movie.comments.create{:text=>'this is realy nice'}#查看评论movie.coments
在这里问你一个问题,上面的listLenght1是一个尾递归还是首递归?让我们来看着这个函数做了哪些事情。
A) 检查参数是否为Nil。
B) 如果为空,返回零,因为Nil的长度是零。
C) 如果不为空,则返回1加上递归调用的结果。
递归调用逻辑在这个函数的最后一行,应该是尾递归函数吧?错!在尾递归调用结束后,然后对递归调用结果加一,然后返回最终结果。这实际上首递归(或者可以叫做中递归)因为递归调用并不是所有运算的最后一步。