Skip to content

Instantly share code, notes, and snippets.

@singpolyma
Created September 18, 2014 15:49
Show Gist options
  • Save singpolyma/1bf25a1a995d000c1270 to your computer and use it in GitHub Desktop.
Save singpolyma/1bf25a1a995d000c1270 to your computer and use it in GitHub Desktop.
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