Created
June 13, 2014 20:15
-
-
Save jgaskins/3b42b05864f0246eb58f to your computer and use it in GitHub Desktop.
Short-circuiting OR/AND in Ruby, represented as methods
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
# If it were possible to override && and || in Ruby, this is how it might look. | |
class NilClass | |
def && other | |
self | |
end | |
def || other | |
other | |
end | |
end | |
class FalseClass | |
def && other | |
self | |
end | |
def || other | |
other | |
end | |
end | |
class BasicObject | |
def && other | |
other | |
end | |
def || other | |
self | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment