?

Log in

No account? Create an account
Overthinking the problem, or another post where I forget the demographics of my readership. - Chaz Meyers [entries|archive|friends|userinfo]
Chaz Meyers

[ website | chazmeyers.com ]
[ profile | view profile ]
[ archive | view archive ]

Links
[Links:| chazmeyers.com Twitter ]

Overthinking the problem, or another post where I forget the demographics of my readership. [Aug. 1st, 2008|02:45 pm]
Chaz Meyers
I was writing some trivial yet repetitive error-handling code when it occurred to me it would be cool if ruby had a feature to define a method that didn't increase the call-stack, such that you could have a new scope but could also return or terminate directly from the calling function. Sort of like the following pseudo-code...

class A
  attr_accessor :check_errors

  def initialize
    @check_errors = Proc.new do |something|
      return something if something.kind_of? Exception
      if something.respond_to? :each
        bad = something.detect { |thing| thing.kind_of? Exception }
        return bad if bad
      end
    end
  end
end

class B < A
  def maybe_erronous
    # do stuff...

    self.check_errors(questionable)

    # do more stuff...
  end
end

Except the code would actually work. If you define a Proc like that it raises a LocalJumpError exception.

Then I remembered I could just re-raise the exception and catch it in the calling code. Duh.
LinkReply