Last active
August 29, 2015 14:02
-
-
Save workmad3/5c19ae87313eb2516c46 to your computer and use it in GitHub Desktop.
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 TrueClass | |
def do(_if: ->{}, _else: ->{}) | |
_if.call | |
self | |
end | |
end | |
class FalseClass | |
def do(_if: ->{}, _else: ->{}) | |
_else.call | |
self | |
end | |
end | |
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 TrueClass | |
def then | |
yield | |
self | |
end | |
def else | |
self | |
end | |
end | |
class FalseClass | |
def then | |
self | |
end | |
def else | |
yield | |
self | |
end | |
end | |
for multiline, how is that any more readable than
if model.persist
# what to do if
# result was positive
# maybe add notes or send an email
else
# it didn't work out. Handle that.
end
? :)
What do you think about 'otherwise' as an alias for else?
find_user_by_email( 'jw@mustmodify.com' ).otherwise do
puts "Sorry, no such user."
end
maybe I'm overthinking.
It isn't any more readable. But it's more OO or, as you said, smalltalky, which makes me happy.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looking at readability:
In terms of readability... I think it's better than a ternary, but the end.else... I wish it could have just been else. I guess using curlies would make it better.