有一种简洁的方法可以在Ruby的循环中返回数值迭代器吗?

Gar*_*all 0 ruby conditional for-loop

似乎应该有一个单行方式迭代seasonseasonshow.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调用,因此我无法真正看到使用它进行迭代的简洁方法.

Pet*_*own 5

你正在做的是一种迂回的回答方式.什么是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)