Top > Ruby [2.2.2] > 条件分岐(case)
★★ 文字列で ★★ a = "234" case a when "123" p 1 when "234" p 2 else p 3 end # 2 ★★ 正規表現で ★★ case a when /123/ p 1 when /234/ p 2 else p 3 end # 2 ★★ 範囲を指定して ★★ case a when "1".."5" p 1 when "6".."9" p 2 else p 3 end # 3 ★★ 条件指定して ★★ a = ["2","3","4"] a.each_with_index do |value, i| case when i == 0 p 1 when value == "4" p 2 else p 3 end end # 1 # 3 # 2