mardi 28 août 2018

How would you refactor this switch-case in Ruby?

How would you refactor this switch-case in Ruby?

I was thinking about hashes, but I am not sure if it would be the best strategy.

def execute_command(input)
   case input.split[0]
   when 'I'
     create(create_hash)
   when 'C'
     clear
   when 'L'
     color(color_hash)
   when 'V'
     line(line_hash)
   when 'S'
     print
   when '?'
     help
   when 'X'
     exit
   else
     error_message
   end
end

Aucun commentaire:

Enregistrer un commentaire