Skip to content

Instantly share code, notes, and snippets.

@grekko
Last active August 29, 2015 14:05
Show Gist options
  • Save grekko/879c10aee3f4fa5f46bd to your computer and use it in GitHub Desktop.
Save grekko/879c10aee3f4fa5f46bd to your computer and use it in GitHub Desktop.
Null Object for Ruby
Gem::Specification.new do |s|
s.name = 'null'
s.version = '0.0.1'
s.platform = Gem::Platform::RUBY
s.author = 'Gregory Igelmund'
s.email = '[email protected]'
s.summary = 'Dynamic Null Object'
s.description = 'Responds on all methods with itself. Credits to @flori. NullObject is taken from Tins: https://github.com/flori/tins'
s.files = ['null.rb']
# TBD => s.test_file = ''
s.require_path = '.'
s.add_development_dependency('rspec', ["~> 2.0"])
end
module Microgem
# Implementation of the null object pattern in Ruby.
class NullObject
def method_missing(*)
self
end
def const_missing(*)
self
end
def to_s
''
end
def to_str
nil
end
def to_f
0.0
end
def to_i
0
end
def to_int
nil
end
def to_a
[]
end
def to_ary
nil
end
def inspect
'NULL'
end
def nil?
true
end
def blank?
true
end
def as_json(*)
end
def to_json(*)
'null'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment