# Arguments into method calls?

**URL:** <https://www.ruby-forum.com/t/arguments-into-method-calls/107873>\
**Category:** Ruby\
**Created:** [June 27, 2007, 1:14am UTC](https://www.ruby-forum.com/t/arguments-into-method-calls/107873 "2007-06-27T01:14:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![torreys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@torreys](https://www.ruby-forum.com/u/torreys)\
**Post date:** [June 27, 2007, 1:14am UTC](https://www.ruby-forum.com/t/arguments-into-method-calls/107873/1 "2007-06-27T01:14:29Z")

</div>

I’m working on a program that takes user input via command line, then  
calls whatever method is related to that input text.

For instance, if they type /help it calls the method help, if they type  
/quit it calls the method quit, /time calls time, etc.

Essentially, if they type in something with a slash, I want the program  
to interpret it as a special command, strip off the / and calls a method  
with the leftover text.

I can think of a clunky way to do this using a ton of if/elsif clauses  
and matching regular expressions, but as I eventually plan to have over  
a hundred commands, that doesn’t seem pleasant to write at all.

Is there a cleaner way to do this?

---

<div class="post-metadata">

**Author:** ![torreys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@torreys](https://www.ruby-forum.com/u/torreys)\
**Post date:** [June 27, 2007, 1:30am UTC](https://www.ruby-forum.com/t/arguments-into-method-calls/107873/2 "2007-06-27T01:30:50Z")

</div>

Torrey Slattery wrote:

> I can think of a clunky way to do this using a ton of if/elsif clauses  
> and matching regular expressions, but as I eventually plan to have over  
> a hundred commands, that doesn’t seem pleasant to write at all.
> 
> Is there a cleaner way to do this?

Check out the **send** method, which lets you call a method whose name  
is in a variable:

obj. **send** (method\_name, args)

---

<div class="post-metadata">

**Author:** ![torreys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@torreys](https://www.ruby-forum.com/u/torreys)\
**Post date:** [June 27, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/arguments-into-method-calls/107873/3 "2007-06-27T16:31:10Z")

</div>

On Jun 27, 4:29 am, Tim H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> RMagick OS X Installer [[http://rubyforge.org/projects/rmagick/](http://rubyforge.org/projects/rmagick/)]  
> RMagick Hints & Tips [[http://rubyforge.org/forum/forum.php?forum\_id=1618](http://rubyforge.org/forum/forum.php?forum_id=1618)]  
> RMagick Installation FAQ [[http://rmagick.rubyforge.org/install-faq.html](http://rmagick.rubyforge.org/install-faq.html)]- Hide quoted text -
> 
> - Show quoted text -

Tim H. is right. Just adding some details:

You can create a hash that has the method names (as strings) as keys,  
and the same names (as symbols) as the corresponding values, e.g.:

method\_map = { “foo” =\> :foo, “bar” =\> :bar }

# Insert code to read and parse your input command here - results in

the command word (without the slash) being stored in the variable  
‘cmd’, and the rest of the args in the variable ‘args’; code is  
straightforward, so omitting for brevity.

# Then:

if method\_map(cmd)  
obj.send(method\_map[cmd], args)  
else  
raise StandardError, “Unrecognized command #{cmd}”  
end

Note: The if condition might be wrong Ruby syntax - I’ve been  
switching between writing Python and Ruby pretty rapidly in the last  
few weeks while working on my projects - and don’t remember right now  
what a reference to a hash element by key returns when the key doesn’t  
exist in the hash (in Ruby, and am not at my machine right now) - I’m  
assuming it returns nil, in which case the code shown should work, but  
could be that I’m confusing it with the way it works in Python - where  
it returns None (Python equivalent to Ruby’s nil) and in Ruby it may  
not return nil but throw an exception or whatever. Sorry about that.  
But shouldn’t be a problem - just check the Ruby docs or even just, at  
an irb prompt, do:

[].methods.sort

This will show you the methods of the Hash class from which it should  
be easy to figure out what method can be used to test for the presence  
of a key in a hash. Modify the if statement accordingly.

An even simpler way may be:

obj.send(cmd.to\_sym, args)

In all the above code, I’m assuming that obj is an object of a class  
that implements all the methods needed to handle the commands  
supported.

HTH  
Vasudev Ram

> **[dancingbison.com](http://ww38.dancingbison.com/)**
>
> Learn Python Programming

> **[jugad's Journal](https://jugad.livejournal.com/)**
>
> Vasudev Ram's blog on software innovation

> **[Conversion of other file formats to PDF](https://sourceforge.net/projects/xtopdf/)**
>
> Download Conversion of other file formats to PDF for free. xtopdf: Tools to convert other formats (x) to PDF; x as in math.

---

<div class="post-metadata">

**Author:** ![torreys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@torreys](https://www.ruby-forum.com/u/torreys)\
**Post date:** [June 27, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/arguments-into-method-calls/107873/4 "2007-06-27T16:36:12Z")

</div>

On Jun 27, 7:26 pm, vasudevram [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > For instance, if they type /help it calls the method help, if they type  
> > > Is there a cleaner way to do this?
> 
> ‘cmd’, and the rest of the args in the variable ‘args’; code is  
> switching between writing Python and Ruby pretty rapidly in the last  
> [].methods.sort  
> that implements all the methods needed to handle the commands  
> supported.
> 
> HTH  
> Vasudev Ramhttp://www.dancingbison.comhttp://jugad.livejournal.comhttp://sourceforge.net/projects/xtopdf- Hide quoted text -
> 
> - Show quoted text -

Forgot to add this (which I checked online before writing the previous  
post):

[http://www.rubycentral.com/book/ref\_c\_object.html](http://www.rubycentral.com/book/ref_c_object.html)

Check out the **send** and send method descriptions at that link.  
**send** is the same as send, its provided as an alternative in case  
your class already has a send method of its own.

- Vasudev
