Created
September 18, 2014 15:49
-
-
Save singpolyma/1bf25a1a995d000c1270 to your computer and use it in GitHub Desktop.
WhyFalsy
This file contains 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 WhyFalsy | |
attr_reader :msg, :backtrace | |
def initialize(msg=nil) | |
@msg = msg | |
# Hack to get a backtrace | |
begin | |
raise | |
rescue Exception | |
@backtrace = $!.backtrace | |
@backtrace.shift # Remove the reference to initialize | |
end | |
end | |
def self.wrap(x, msg=nil) | |
# The !! makes fake-falsy values (like us) work | |
!!x ? x : self.new(msg) | |
end | |
# This is so you can convert to bool with !! | |
def !@ | |
true | |
end | |
def blank? | |
true | |
end | |
def or | |
yield | |
end | |
def and | |
self | |
end | |
end | |
class FalseClass | |
def or | |
yield | |
end | |
def and | |
self | |
end | |
end | |
class NilClass | |
def or | |
yield | |
end | |
def and | |
self | |
end | |
end | |
class Object | |
def or | |
self | |
end | |
def and | |
yield | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment