Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class NullObject < BasicObject
def initialize(represents = nil)
@represents = represents || 'NullObject'
end
def nil?; true; end
def present?; false; end
def blank?; true; end
def empty?; true; end
def !; true; end
def to_a; []; end
def to_s; ''; end
def to_f; 0.0; end
def to_i; 0; end
def to_str; to_s; end
def inspect
"<#{represents}>"
end
def method_missing(*args, &block)
self
end
private
attr_reader :represents
end
date = nil || NullObject.new('NullDate')
date.inspect # => '<NullDate>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment