Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 903
Toby Joiner Avatar asked Jun 13 '11 21:06

Toby Joiner


1 Answers

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
like image 72
JCorcuera Avatar answered Sep 19 '22 00:09

JCorcuera