Skip to content

Instantly share code, notes, and snippets.

@stephenaument
Last active August 29, 2015 13:57
Show Gist options
  • Save stephenaument/9422568 to your computer and use it in GitHub Desktop.
Save stephenaument/9422568 to your computer and use it in GitHub Desktop.
2014-03-11 Null Object Part 2
class NilDuck < NilObject
def name
'Demo Duck'
end
def status
'sleeping'
end
def color
'gray'
end
def migratory?
true
end
end
class NilDuck < NilObject
def initialize
super(Duck)
end
def name
'Demo Duck'
end
def status
'sleeping'
end
def color
'gray'
end
def migratory?
true
end
end
class NilDuck
def name
'Demo Duck'
end
def status
'sleeping'
end
def color
'gray'
end
def migratory?
true
end
def method_missing(name, *args)
super unless Duck.new.respond_to?(name)
name.ends_with?('?') ? false : nil
end
end
class NilGoose < NilObject
def name
'Demo Goose'
end
def status
'awake'
end
def color
'black'
end
def migratory?
true
end
end
class NilGoose
def name
'Demo Goose'
end
def status
'awake'
end
def color
'black'
end
def migratory?
true
end
def method_missing(name, *args)
super unless Goose.new.respond_to?(name)
name.ends_with?('?') ? false : nil end
end
class NilGoose < NilObject
def initialize
super(Goose)
end
def name
'Demo Goose'
end
def status
'awake'
end
def color
'black'
end
def migratory?
true
end
end
class NilObject
def initialize(klass)
@klass = klass
end
def method_missing(name, *args)
return super unless @klass.new.respond_to?(name)
name.to_s.ends_with?('?') ? false : nil
end
end
class NilObject
def initialize(klass=nil)
@klass = klass || real_class_name
end
def method_missing(name, *args)
return super unless @klass.new.respond_to?(name)
name.to_s.ends_with?('?') ? false : nil
end
def real_class_name
self.class.name[3..-1].constantize
end
end
class NilObject
def initialize(klass=nil)
@klass = klass || real_class_name
end
def method_missing(name, *args)
return super unless respond_to?(name)
name.to_s.ends_with?('?') ? false : nil
end
def respond_to?(name)
@klass.new.respond_to?(name)
end
def real_class_name
self.class.name[3..-1].constantize
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment