# Method declaration question

**URL:** <https://www.ruby-forum.com/t/method-declaration-question/131091>\
**Category:** Rails\
**Created:** [March 5, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/method-declaration-question/131091 "2008-03-05T23:43:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 5, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/method-declaration-question/131091/1 "2008-03-05T23:43:21Z")

</div>

Hi-

In auto\_complete, a method is declared as follows:

def auto\_complete\_for(object, method, options = {})

Does the second argument mean I can send anything I want to this  
method after I send ‘object’?

If so, shouldn’t I be able to call it like this:

auto\_complete\_for(something,:test =\> “person”) ?

I try exactly this and get

Unknown key(s):

Can someone illuminate me on declaring “something = {}” as an argument  
and how you call that method with passing in bonus arguments?

Thanks,  
Dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 6, 2008, 12:03am UTC](https://www.ruby-forum.com/t/method-declaration-question/131091/2 "2008-03-06T00:03:20Z")

</div>

Hi –

On Wed, 5 Mar 2008, dino d. wrote:

> If so, shouldn’t I be able to call it like this:
> 
> auto\_complete\_for(something,:test =\> “person”) ?
> 
> I try exactly this and get
> 
> Unknown key(s):
> 
> Can someone illuminate me on declaring “something = {}” as an argument  
> and how you call that method with passing in bonus arguments?

Given this:

def m(a, b, c = {})

a and b are required arguments, and have to come in that order. c is  
an optional argument; if you don’t provide it, c will be assigned an  
empty hash (or whatever you put there as the default value).

In your case, the hash { :test =\> “person” } is being assigned to  
method, and c is an empty hash. Basically, you’re missing an argument.

David

> Thanks,  
> Dino
> 
> >

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS, April 14-17 2008, New York City  
CORE RAILS, June 24-27 2008, London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details. Berlin dates coming soon!

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 6, 2008, 1:07am UTC](https://www.ruby-forum.com/t/method-declaration-question/131091/3 "2008-03-06T01:07:15Z")

</div>

Thanks for the reply. Sorry, I typed my original post too fast. This  
is the declaration:

> > def auto\_complete\_for(object, method, options = {})

My call to the method is:

auto\_complete\_for :story, :tag\_holder, :foreign =\> ‘tag’

and this usage throws this error:

ArgumentError (Unknown key(s): foreign):

Any idea why?

Thanks, and sorry for the rushed post.

Dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 6, 2008, 1:55am UTC](https://www.ruby-forum.com/t/method-declaration-question/131091/4 "2008-03-06T01:55:55Z")

</div>

On 3/5/08, dino d. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> auto\_complete\_for :story, :tag\_holder, :foreign =\> ‘tag’
> 
> and this usage throws this error:
> 
> ArgumentError (Unknown key(s): foreign):
> 
> Any idea why?
> 
> Thanks, and sorry for the rushed post.

Well auto\_complete\_for is taking the options hash, which in your case  
is {:foreign =\> ‘tag’} and merging it into the options passed to  
ActiveRecord::Base#find, so the following effective expression gets  
invoked:

```
Story.find(:all, :conditions => [ "LOWER(tag_holder) LIKE ?", '%'

```

- params[:story][:tag\_holder].downcase + ‘%’ ],  
:order =\> “#{tag\_holder} ASC”,  
:limit =\> 10,  
:foreign =\> ‘tag’)

And ActiveRecord::Find is rejecting :foreign since it’s not a valid  
option.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 6, 2008, 1:43am UTC](https://www.ruby-forum.com/t/method-declaration-question/131091/5 "2008-03-06T01:43:36Z")

</div>

Hi –

On Wed, 5 Mar 2008, dino d. wrote:

> and this usage throws this error:
> 
> ArgumentError (Unknown key(s): foreign):
> 
> Any idea why?

The options hash gets merged into the find options for a database  
query, so it has to be something that’s meaningful in that context.  
You could, for example, put :limit =\> 20, which would override the  
default limit of 10. But :foreign =\> ‘tag’ doesn’t mean anything in an  
ActiveRecord query.

David

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS, April 14-17 2008, New York City  
CORE RAILS, June 24-27 2008, London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details. Berlin dates coming soon!

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 6, 2008, 2:22am UTC](https://www.ruby-forum.com/t/method-declaration-question/131091/6 "2008-03-06T02:22:17Z")

</div>

Thanks for the replies. How do I specify my own hasg? i.e. can a  
method take two hashes, and if so, how do I specify which hash an  
option goes into whne called?

ex)

def mymethod(a,b, c={}, d={})

how do i specify the call?

Thanks again,  
Dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 6, 2008, 2:33am UTC](https://www.ruby-forum.com/t/method-declaration-question/131091/7 "2008-03-06T02:33:25Z")

</div>

Hi –

On Wed, 5 Mar 2008, dino d. wrote:

> Thanks for the replies. How do I specify my own hasg? i.e. can a  
> method take two hashes, and if so, how do I specify which hash an  
> option goes into whne called?
> 
> ex)
> 
> def mymethod(a,b, c={}, d={})
> 
> how do i specify the call?

What you’ve got there are two required arguments and two optional  
arguments. If you call that method with all four args specified, you’d  
do:

mymethod(1,2, { :m =\> “n” }, :x =\> “y”)

That will give the following assignments:

a 1  
b 2  
c { :m =\> “n” }  
d { :x =\> “y” }

Ruby has the special rule that if a hash is the last thing in the  
argument list, you can leave off the curly braces. But the third  
argument, in this scenario, is _not_ the last argument, so it needs  
its curly braces. Otherwise it is, as I think you’ve surmised,  
impossible to tell where one hash is supposed to end and the next one  
begin.

David

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS, April 14-17 2008, New York City  
CORE RAILS, June 24-27 2008, London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details. Berlin dates coming soon!

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 6, 2008, 3:16am UTC](https://www.ruby-forum.com/t/method-declaration-question/131091/8 "2008-03-06T03:16:48Z")

</div>

> Ruby has the special rule that if a hash is the last thing in the  
> argument list, you can leave off the curly braces.

_This_ is a critical missing piece of knowledge for me. Thanks so  
much. Btw, I’ve found learnng basic rails pretty easy, but learning  
intermediate rails extremely difficult because I can’t find a good  
book to take me there. Can you recommend one? How did you get from  
beginner to intermediate to expert?

Thanks,  
Dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 6, 2008, 1:46pm UTC](https://www.ruby-forum.com/t/method-declaration-question/131091/9 "2008-03-06T13:46:32Z")

</div>

Hi –

On Wed, 5 Mar 2008, dino d. wrote:

> > Ruby has the special rule that if a hash is the last thing in the  
> > argument list, you can leave off the curly braces.
> 
> _This_ is a critical missing piece of knowledge for me. Thanks so  
> much. Btw, I’ve found learnng basic rails pretty easy, but learning  
> intermediate rails extremely difficult because I can’t find a good  
> book to take me there. Can you recommend one? How did you get from  
> beginner to intermediate to expert?

The key for me was that I had been using Ruby for almost four years  
before Rails was even released, and that I understood automatically  
that writing a Rails application is actually a process of writing lots  
of little Ruby programs. There’s nothing secret about this, of course,  
though sometimes the Rails magic and high-level techniques can make it  
seem like you’re just floating on an abstract layer where you don’t  
really have to think about what the syntax actually means or what the  
objects are doing. As you see from the case of the hash argument  
idiom, that’s not true – at least not if you want to get past that  
beginning stage.

As for books, there’s “Ruby for Rails” (by me), which is a Ruby  
intro/guide with topics selected with an eye toward the Rails  
developer. The Rails examples in it are based on Rails 1.something, so  
I wouldn’t recommend it as a single source of Rails knowledge (which  
wasn’t its purpose anyway), but the Ruby is 1.8 and therefore still  
the Ruby that almost all Rails developers use. There’s also “The Rails  
Way”, by Obie F., which I think would be a very good resource.  
There are probably others; I’m not fully up to date these days on the  
book scene, as there are so many – which is great, but makes it hard  
to keep up!

David

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS, April 14-17 2008, New York City  
CORE RAILS, June 24-27 2008, London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details. Berlin dates coming soon!
