Skip to content

Instantly share code, notes, and snippets.

@warmwaffles
Created March 31, 2015 01:09
Show Gist options
  • Save warmwaffles/e5b3e03395cff4512e0e to your computer and use it in GitHub Desktop.
Save warmwaffles/e5b3e03395cff4512e0e to your computer and use it in GitHub Desktop.
# A basic null object that can be used in place of a `nil`.
class NullObject
def initialize(*); end
def method_missing(*); self; end
def respond_to?(*); true; end
def to_s; ''; end
def to_str; ''; end
def to_i; 0; end
def to_f; 0.0; end
def to_c; 0.to_c; end
def to_r; 0.to_r; end
def to_a; []; end
def to_ary; []; end
def to_h; {}; end
def nil?; true; end
def present?; false; end
def persisted?; false; end
def empty?; true; end
def blank?; true; end
def !; true; end
def tap
yield(self)
self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment