1

可能重复:
如何在 Ruby 中打破外循环?

说我有这个代码:

class A

  def initialize
    myMethod()
    print "This should not be printed"
  end

  def myMethod
    #here
  end

end

obj = A.new
print "This should be printed"

是否有任何命令可以代替“#here”退出“obj”对象并继续下一条语句?(打印“这应该打印”)

4

1 回答 1

3

throw/catch 会这样做:

class A

  def initialize
    catch :init_done do
      myMethod()
      print "This should not be printed"
    end
  end

  def myMethod
    throw :init_done
  end

end

obj = A.new
print "This should be printed"
于 2012-10-13T12:56:43.593 回答