Chaz Meyers (cpm) wrote,
Chaz Meyers

Overthinking the problem, or another post where I forget the demographics of my readership.

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 = 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

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


    # do more stuff...

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.

  • My tweets

    Mon, 08:57: RT @ thomdunn: New inspiration just dropped

  • My tweets

    Fri, 23:43: RT @ TheParkerJam: Have any fellow EST @ Fusion fans been following this year? Between new time zone and the gaps between games, I…

  • My tweets

    Fri, 10:53: RT @ TheParkerJam: I can now say I'm in an Among Us YouTube video that has features Kimi, Ellum, and Chilled. 🤯(OK, it's a…

  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.