Last active
August 29, 2015 14:04
-
-
Save michaelrkn/8c53dc7ef0569d2ffd9e to your computer and use it in GitHub Desktop.
implement if, then, and else as methods
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 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