Skip to content

Instantly share code, notes, and snippets.

@michaelrkn
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelrkn/8c53dc7ef0569d2ffd9e to your computer and use it in GitHub Desktop.
Save michaelrkn/8c53dc7ef0569d2ffd9e to your computer and use it in GitHub Desktop.
implement if, then, and else as methods
class Object
def _if(object)
object.class != FalseClass && !object.nil?
end
end
class TrueClass
def then
yield
self
end
def else
self
end
end
class FalseClass
def then
self
end
def else
yield
self
end
end
_if(7 > 5).then do
puts "Math is right"
end.else do
puts "Math is wrong"
end
_if(5 > 7).then do
puts "Math is wrong"
end.else do
puts "Math is right"
end
_if("truthy").then do
puts "Strings are truthy"
end.else do
puts "Strings are falsey"
end
_if(nil).then do
puts "nil is truthy"
end.else do
puts "nil is falsey"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment