method declaration question

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

Hi --

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?

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 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

Hi --

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.

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

Hi --

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

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

Hi --

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 Fernandez, 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