case when with match
Is there a way to make this work correctly with a case when?
field = "head_count_2011_10_75"
case 开发者_开发百科field
when match(/head_count_\d{4}_\d{1,2}_\d{1,4}/i)
puts "regex 1"
when match(/dmi_\d{4}_\d{1,2}_\d{1,4}/i)
puts "regex 2
end
I know I can do it with if:
if field.match(/head_count_\d{4}_\d{1,2}_\d{1,4}/i)
puts "regex 1"
elsif field.match(/dmi_\d{4}_\d{1,2}_\d{1,4}/i)
puts "regex 2"
end
Just looking for a cleaner solution.
Just remove the match:
field = "head_count_2011_10_75"
case field
when /head_count_\d{4}_\d{1,2}_\d{1,4}/i
puts "regex 1"
when /dmi_\d{4}_\d{1,2}_\d{1,4}/i
puts "regex 2
end
精彩评论