From: Charles Hixson Date: 2013-04-19T10:52:10+09:00 Subject: Re: Match against multiple patterns problem On 04/18/2013 11:37 AM, Joel Pearson wrote: > How about this? > > def test(s) > return ['1',''] if !s.is_a?(String) || s.empty? > case s > when /^([^\W_]+)/ > ['a', $1] > when /^(\s+)/ > ['s', $1] > when /^([[:cntrl:]]+)/ > ['c', $1] > when /^([[:punct:]_]+)/ > ['p', $1] > when /^([^\w\s[:cntrl:][:punct:]]+)/ > ['r', $1] > end > end > Given that $1 means what I think it means from context, that would work, but I dislike the global variables with pattern matching. I understand that some people consider assignment within an if statement to be worse, but I'm not one of them. Were I to use that form, I'd indent it differently (maybe you normally do). Also, there would need to be a default case to handle uncovered options (which just prints an error message and returns ["2", ""]. empty? instead of a length test is reasonable, and I should think about adopting that change. But really my problem was being mislead by error messages about what the problem actually was. -- Charles Hixson