以下の内容はhttps://tbpgr.hatenablog.com/entry/20120724/1343150559より取得しました。


書籍 リーダブルコード | 三項演算子

内容

三項演算子を利用する場合はコードが簡潔な場合に限る。

サンプルコード

def is_hoge(target)
  return target == "hoge";
end

%w [hoge hage hige].each do |each_word|
# 3項演算子だとシンプルになるケース
  puts is_hoge(each_word)? "YES!hoge!!":"No・・・Not hoge"
end

%w [hoge hage hige].each do |each_word|
# 3項演算子だと複雑になるケース
  puts is_hoge(each_word)? each_word.upcase + ":ほげ":each_word.downcase + ":はげorひげ"
end

出力

YES!hoge!!
No・・・Not hoge
No・・・Not hoge
HOGE:ほげ
hage:はげorひげ
hige:はげorひげ



以上の内容はhttps://tbpgr.hatenablog.com/entry/20120724/1343150559より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14