Skip to content

Instantly share code, notes, and snippets.

@hjr3
Last active December 10, 2015 21:28
Show Gist options
  • Save hjr3/4494969 to your computer and use it in GitHub Desktop.
Save hjr3/4494969 to your computer and use it in GitHub Desktop.
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