Last active
August 29, 2015 14:13
-
-
Save hagiyat/f80e58db7c7928b43eb5 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
cond1 = false | |
cond2 = true | |
# nested ternary operater | |
cond1 ? 'piyo' : (cond2 ? 'baz' : 'bar') | |
# use lambda | |
[->{'piyo' if cond1}, ->{'baz' if cond1 and cond2}, ->{'bar'}].lazy.map(&:call).select(&:present?).first | |
=> 'bar' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment