Skip to content

Instantly share code, notes, and snippets.

@hagiyat
Last active August 29, 2015 14:13
Show Gist options
  • Save hagiyat/f80e58db7c7928b43eb5 to your computer and use it in GitHub Desktop.
Save hagiyat/f80e58db7c7928b43eb5 to your computer and use it in GitHub Desktop.
三項演算子のネストを解消しつつワンライナーで書く
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