【问题标题】:exit from all methods in an object in ruby [duplicate]退出红宝石对象中的所有方法[重复]
【发布时间】:2012-10-04 01:56:09
【问题描述】:

可能重复:
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”对象并继续下一条语句吗? (print "This should be print")

【问题讨论】:

标签: ruby oop class object exit


【解决方案1】:

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"

【讨论】:

  • 这并不是我所希望的,但我自己做了一些研究,看起来没有更好的方法,所以谢谢你的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-19
  • 1970-01-01
  • 2015-03-21
  • 1970-01-01
  • 2015-08-06
  • 1970-01-01
相关资源
最近更新 更多