I'm trying to write a shell in Ruby (using readline
), implementing some custom commands. To create the correspondence between given input and methods that are defined in an external module I'm using an hash like this
hash = {
'command_1' => method(:method_1),
'command_2' => method(:method_2)
}
And once I got the user input I pass it to the hash calling the method associated with the command-key
hash[input.to_s].()
My questions are: how can I handle variants of the same command? (e.g. for the help
command give different output depending if a flag is given, something like help -command_1
)
How can I pass parameters to methods in the hash? (e.g. pass to an open
command the file to be opened like open file_name
)
Thanks to all in advance.