可能的重复项:
如何在 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"对象并继续下一条语句?(打印"这应该打印")
投掷/接球就可以了:
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"