Created
August 12, 2013 14:56
-
-
Save wjlroe/6211533 to your computer and use it in GitHub Desktop.
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
class NullObject | |
def method_missing(*args, &block) | |
self | |
end | |
def nil?; true; end | |
end | |
def Maybe(value) | |
value.nil? ? NullObject.new : value | |
end | |
foo = nil | |
buz = nil | |
Maybe(foo).bar.baz + buz | |
# => #<NullObject:0x007fad649f68c0> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Breaks however, if
Maybe(foo).bar
returns a real object and that object doesn't respond to#baz
then you getNoMethodException
rather than your intendednil