Skip to content

Instantly share code, notes, and snippets.

@rkh
Created March 19, 2012 12:53
Show Gist options
  • Save rkh/2110987 to your computer and use it in GitHub Desktop.
Save rkh/2110987 to your computer and use it in GitHub Desktop.
def lazy
yield
rescue NameError
Module.new
end
begin
raise "foo"
rescue lazy { Foo }, lazy { Exception }
end
@judofyr
Copy link

judofyr commented Mar 19, 2012

Or use a null object:

class Null
  def self.===(o) false end
end

def lazy
  yield
rescue NameError
  Null
end

@rkh
Copy link
Author

rkh commented Mar 19, 2012

Yeah, or that. Performance optimizing your rescue clause.

@judofyr
Copy link

judofyr commented Mar 19, 2012

I don't always rescue exceptions, but when I do it's SUPER FAST.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment