Gar*_*all 0 ruby conditional for-loop
似乎应该有一个单行方式迭代season并season在show.has_season?评估时返回最后一个false.
def last_season(show)
season = 1
season += 1 while show.has_season?(season)
return season
end
Run Code Online (Sandbox Code Playgroud)
编辑: has_season?涉及HTTP GET调用,因此我无法真正看到使用它进行迭代的简洁方法.
你正在做的是一种迂回的回答方式.什么是has_season?方法在做什么?你的节目对象有一系列季节会有意义吗?然后你可以做一些像:
class Show
attr_accessor :seasons
def initialize
@seasons = []
end
def last_season
seasons.last
end
end
show = Show.new
show.seasons = [1,2,3,4,5,6]
show.last_season # => 6
Run Code Online (Sandbox Code Playgroud)