Last active
December 10, 2015 21:28
-
-
Save hjr3/4494969 to your computer and use it in GitHub Desktop.
A cardinal in Ruby 1.9 See https://github.com/raganwald/homoiconic/blob/master/2008-10-31/songs_of_the_cardinal.markdown
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 cardinal_define(name, &proc_over_proc) | |
self.class.send(:define_method, name) do |a_value, &a_proc| | |
proc_over_proc.call(a_proc).call(a_value) | |
end | |
end | |
cardinal_define(:maybe) do |a_proc| | |
lambda { |a_value| | |
a_proc.call(a_value) unless a_value.nil? | |
} | |
end | |
maybe(1) do |x| | |
x + 1 | |
end | |
#=> 2 | |
maybe(nil) do |x| | |
x + 1 | |
end | |
#=> nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment