Topic: Ruby syntax question

        
<%= link_to recipe.title,
            :action => "show",
            :id => recipe.id %>

That code is used in a ruby on rails tutorial on this page: http://www.onlamp.com/pub/a/onlamp/2005 … tml?page=2

I understand what is happening , but
I don't understand the syntax involved. 
This is more of a Ruby question than a Ruby on Rails question, but I'd
appreciate it if you could shine some light on the syntax rules here.
Is link_to a method with the next three comma-separated terms being the paramaeters?
And I thought => was hash syntax used to bind keys to values, but are the last two
terms part of hashes?  And what's with the weird colon in front of the names?

I've been skimming this book: http://www.rubycentral.com/book/
But I've really been bouncing around and not reading it front to back.  So maybe
if you could just tell me what subject I should look into to figure this out.(e.g. method calls, hash syntax, etc.)

Thanks.

Re: Ruby syntax question

link_to is a method. You can look it up in the dox (http://api.rubyonrails.org/classes/Acti … ml#M000378)

link_to(name, options={}, html_options=nil, *parameters_for_method_reference)
What's happening is you are putting what want to appear on the page in 'name', the next batch of things are normally the options hash.

E.g., :action=>'list', :id=>@my_object

Finally, you may get to html_options, which would include things like :post => true, :confirm => 'are you sure?'

The syntax that is confusing is that Ruby has figured out that the first argument can only be a string and has gobbled it up. Because the trailing arguments all follow hash syntax, Ruby decides to dump as many as possible into the options hash. This is great because most cases don't rely on further arguments, but say you do want to use an html_options argument. Then you have to write:

link_to('delete', {action => 'destroy', :id => @my_object}, :confirm => 'are you sure', :post => true)

Hope this clears it up a bit...

Re: Ruby syntax question

"And what's with the weird colon in front of the names?"

Yeah, that took some "getting used to" for me, too.

Here's the way I view it (I'm sure I'll be corrected wherever I'm wrong):

They're symbols, and I think of them as 'lite' versions of strings. Since everything in Ruby is an object, a string instance has many built-in methods that require memory.

Technically, you can use strings as your hash keys, but you're creating a new instance of that string every time you invoke it. If you use a symbol, you're only creating a 'pointer' to a string, so you can create as many symbols of the same name as you want without creating unnecessary instance methods.

For hash keys (and other common strings) that you'll probably use over and over again, this can result in savings. Hash values tend to be unique, so it's just as well to use strings for them.

Re: Ruby syntax question

That's the engineering answer; the lazy answer is that :foo is one less character to type than "foo"; some astheticly minded people argue that :foo is sleeker than "foo" as the literal definer is more or less centered on the literal as opposed to being "above" the normal eye flow.

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: Ruby syntax question

Thanks for explaining about symbols. After I learned that their called symbols I found this: http://www.oreillynet.com/ruby/blog/200 … by_sy.html

That makes it pretty clear.  I don't know in how many contexts a symbol can be used, but it kind of strikes me as being a sort of "key type".

Re: Ruby syntax question

cwd wrote:

link_to is a method. You can look it up in the dox (http://api.rubyonrails.org/classes/Acti … ml#M000378)

link_to(name, options={}, html_options=nil, *parameters_for_method_reference)
What's happening is you are putting what want to appear on the page in 'name', the next batch of things are normally the options hash.

[snip]

Hope this clears it up a bit...

Yeah, that helped.  I think it would probably be better to always use
the braces though, just for clarity.  It's only two more characters.