退出ruby中对象中的所有方法

时间:2012-10-13 11:48:23

标签: ruby oop class object exit

  

可能重复:
  How to break outer cycle in 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'对象并继续下一个语句? (打印“这应该打印”)

1 个答案:

答案 0 :(得分: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"