Created
September 9, 2020 01:47
-
-
Save dmshvetsov/cbcc6dd016881188be9e2f988dc4c41e to your computer and use it in GitHub Desktop.
How to avoid if/else branch condition in Ruby. By Yehuda Katz https://yehudakatz.com/2009/10/04/emulating-smalltalks-conditionals-in-ruby/. Please don't do it in production code or your gems.
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
class Object | |
def if_true | |
yield | |
self | |
end | |
def if_false | |
self | |
end | |
end | |
class NilClass | |
def if_true | |
self | |
end | |
def if_false | |
yeild | |
self | |
end | |
end | |
class FalseClass | |
def if_true | |
self | |
end | |
def if_false | |
yeild | |
self | |
end | |
end | |
# Then it make possible | |
something.if_true { | |
# The "true" case | |
}.if_false { | |
# The "false" case | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment