Last active
December 11, 2015 07:58
-
-
Save avit/4569725 to your computer and use it in GitHub Desktop.
Wondering why a direct comparison using "if" passes when the value is falsy.
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 NullObject | |
def true?; false end | |
def false?; true end | |
def nil?; true end | |
def blank?; true end | |
def present?; false end | |
def empty?; true end | |
def to_s(*); "" end | |
def !; true end | |
def method_missing(*args, &block) | |
self | |
end | |
def to_value | |
nil | |
end | |
end | |
if NullObject.new | |
puts "I should not be here, isn't it falsy?" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment