ruby 从方法内部调用next for outer循环

qco9c6ql  于 2023-05-28  发布在  Ruby
关注(0)|答案(1)|浏览(107)

是否可以在外部循环的方法中调用next:

bot.rb

while Option.daemon_active?(daemon: "bot")
  .....
  Trade.market_order
  ....
end

trade.rb

class Trade
  def self.market_order
     ... complex code ...
     response = exchange.market_sell
     next if response["state"] == false # This fails. This should start new iteration of while in bot.rb
  end
end

有一个非常相似的问题,但它似乎不适合我:call next on ruby loop from external method

w8ntj3qf

w8ntj3qf1#

是的。您应该使用throwcatch
bot.rb

while Option.daemon_active?(daemon: "bot")
  catch(:foo) do
    ...
    Trade.market_order
    ...
  end
end

trade.rb

class Trade
  def self.market_order
    ...
    response = exchange.market_sell
    throw :foo if response["state"] == false
  end
end

相关问题