Instantly share code, notes, and snippets.

Embed
What would you like to do?
WhyFalsy
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