Skip to content

Instantly share code, notes, and snippets.

@jnunemaker
Created April 15, 2012 13:38
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 jnunemaker/2392840 to your computer and use it in GitHub Desktop.
Save jnunemaker/2392840 to your computer and use it in GitHub Desktop.
basic null object
# from http://avdi.org/talks/confident-code-rubymidwest-2011/confident-code.html
class NullObject
def method_missing(*)
self
end
def nil?
true
end
end
def Maybe(value)
value.nil? ? NullObject.new : value
end
class SomeClass
def initialize(logger = NullObject.new)
@logger = logger
end
end
@jnunemaker
Copy link
Author

The basic idea is nil is over used. It means error or missing data or flag for default behavior or uninitialized variable or default return value for some methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment