Skip to content

Instantly share code, notes, and snippets.

@shouichi
Created August 23, 2011 08:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shouichi/1164633 to your computer and use it in GitHub Desktop.
Save shouichi/1164633 to your computer and use it in GitHub Desktop.
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