On 12/20/2013 02:57 AM, Previn Lin wrote:
> Is there better way to replace these case statement?
>
> case cmd
>            when /^Astro$/
>                  toolName = 'astro'
>
>            when /^core/
>                  toolName = 'coretools'
>
>            when /^(fm_shell|formality)$/
>                  toolName = 'fm'
>
>            when /^gca_shell$/
>                  toolName = 'gcas'
>      ......
>
>            when /^xa/
>                  toolName = 'xa'
>
>            else
>                  toolName = ''
>
> end
>


class Tool
   attr_reader :name, :pat

   def initialize name, pat
     @name = name
     @pat = pat
   end

   def === cmd
     @pat === cmd
   end
end

astro = Tool.new 'astro', /^Astro$/
core = Tool.new 'coretools', /^core/
fm = Tool.new 'fm', /^(fm_shell|formality)$/
gcas = Tool.new 'gcas', /^gca_shell$/

tools = [astro, core, fm, gcas]

cmd = "corezap"
puts tools.find {|tool| tool === cmd}.name

cmd = "gca_shell"
case cmd # there is a special relationship between case and #===
when astro; puts "doing astro stuff"
when gcas; puts "doing gcas stuff"
end