cmd="Astro"

#decode
code_book={ Astro: "astro", core: "coretools", fm_shell: "fm", formality: "fm" }
tool_name = code_book[cmd.to_sym]

#output  
puts tool_name

The downside I see here is it returns nil instead of "" if there is no matching.

J.T

-----Original Message-----
From: ruby-talk [mailto:ruby-talk-bounces / ruby-lang.org] On Behalf Of Previn Lin
Sent: Friday, December 20, 2013 2:58 AM
To: ruby-talk / ruby-lang.org
Subject: Is there better way to replace these case statement?

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

-- 
Posted via http://www.ruby-forum.com/.