1

Possible Duplicate:
How to break outer cycle in Ruby?

say I have this code:

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"

Is there any command that I can place instead of "#here" that would exit the 'obj' object and continue to the next statement? (print "This should be printed")

Community
  • 1
  • 1
Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55
  • possible duplicate of [How to break outer cycle in Ruby?](http://stackoverflow.com/questions/1352120/how-to-break-outer-cycle-in-ruby), http://stackoverflow.com/questions/4988045 – sawa Oct 13 '12 at 12:03

1 Answers1

3

throw/catch will do it:

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"
Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191
  • this isn't exactely what I was hoping for but I've done a little research myself and it looks like there is no better way so thanks for the answer. – Ionut Hulub Oct 13 '12 at 12:59