Created
August 23, 2011 08:23
-
-
Save shouichi/1164633 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
module Maybe | |
class NullObject | |
def initialize | |
@origin = caller.first | |
end | |
def __null_origin__ | |
@origin | |
end | |
def method_missing(*args, &block) | |
self | |
end | |
def nil? | |
true | |
end | |
end | |
def self.maybe(obj) | |
obj.nil? ? NullObject.new : obj | |
end | |
end | |
def maybe(obj) | |
::Maybe.maybe(obj) | |
end | |
p maybe(nil).foo.bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment