Skip to content

Instantly share code, notes, and snippets.

@matsumonkie
Created June 13, 2014 22:18
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 matsumonkie/e8b6b1e9407b5d67b97c to your computer and use it in GitHub Desktop.
Save matsumonkie/e8b6b1e9407b5d67b97c to your computer and use it in GitHub Desktop.
fallback
#------------------------------------------------------------------------
def Fallback(lambda, &fallback)
fallback ||= -> { raise }
begin
lambda.call()
rescue
fallback.call()
end
end
require 'active_support/core_ext'
def date_of_birth(age:)
birth_date = Date.today - age.year
birth_date.year
end
p Fallback -> { date_of_birth(age: 25) }
p Fallback -> { date_of_birth(age: '25') } { 'probably before 2000' }
p Fallback -> { date_of_birth(age: '25') }
# 1989
# "probably before 2000"
# fallback.rb:13:in `date_of_birth': undefined method `year' for "25":String (NoMethodError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment