Last active
August 29, 2015 13:57
-
-
Save stephenaument/9422568 to your computer and use it in GitHub Desktop.
2014-03-11 Null Object Part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class NilDuck < NilObject | |
| def name | |
| 'Demo Duck' | |
| end | |
| def status | |
| 'sleeping' | |
| end | |
| def color | |
| 'gray' | |
| end | |
| def migratory? | |
| true | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class NilGoose < NilObject | |
| def name | |
| 'Demo Goose' | |
| end | |
| def status | |
| 'awake' | |
| end | |
| def color | |
| 'black' | |
| end | |
| def migratory? | |
| true | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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