Created
June 13, 2014 22:18
-
-
Save matsumonkie/e8b6b1e9407b5d67b97c to your computer and use it in GitHub Desktop.
fallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------------------ | |
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