Skip to content

Instantly share code, notes, and snippets.

@coderdave
Created April 24, 2015 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderdave/9af1a5593d8fb00f793b to your computer and use it in GitHub Desktop.
Save coderdave/9af1a5593d8fb00f793b to your computer and use it in GitHub Desktop.
Ruby nil_chain FTW
module Kernel
def nil_chain(ret_val = nil, &block)
begin
result = yield
return ret_val if result.nil?
result
rescue NoMethodError
rescue NameError
return ret_val
end
end
end
@coderdave
Copy link
Author

Stop using try!

Full disclosure, this is a complete rip off from https://github.com/forgecrafted/finishing_moves

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