Topic: wierd syntax i want to understand

Hello all,

Through working with rails i've come across a couple of things that i really don't understand synactically speaking.

First one is when doing this:

@hello = sessions[:name]

:name is wierd here because since am using sessions array i should be accessing index-based item. So how come i am using :name?

Second one is almost the same when doing:

<%= f.text_area :description %>

This is a method call which should take arguments, what is :description and what does it represent?

Regards,
Rakan

Re: wierd syntax i want to understand

sessions is a Hash, not an Array

A Hash is like an array,  except instead of integer indexes,  you access it by a key,  commonly symbols are used as the key for the hash.

<%= f.text_area :description %>

Is in fact a call to the text_area form helper with an argument, it could of been written

<%= f.text_area(:description) %>

It's passing a symbol as an argument.  In this case it's telling the text_area helper to put up a text_area on the :description attribute in the model. 

You could of written it like this:

<%= f.text_area('description') %>

<%= f.text_area 'description' %>

That works also,  but using symbols is a tad more efficient 'under the covers' of RoR.

DONT define new symbols for the first time routes.rb!!!

:action=>'mynewaction' is cool
:action=>:mynewaction is not

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: wierd syntax i want to understand

I have one as well. What does the &:symbol mean? You can imagine that I've had a hard time looking this up.

@chash = @version.directly_uses.group_by(&:artifact)

Re: wierd syntax i want to understand

It's hard to look up because I believe both group_by and the &:symbol methods were introduced in Rails.

Some railscast episodes on these:
http://railscasts.com/episodes/6-shortc … ol-to-proc
http://railscasts.com/episodes/29-group-by-month

The symbol to_proc method is going to be very useful tongue

Last edited by Kentor (2010-07-02 19:02:09)

Re: wierd syntax i want to understand

BradHodges wrote:

DONT define new symbols for the first time routes.rb!!!

What happens if I do?

Re: wierd syntax i want to understand

This was based on earlier rails, 2.2.?,  not sure if it's still the case.

Because the Routing module is setup so early in the Rails boot-up process,  it would mess things up if you defined something with a new symbol, introduced by you into routes.rb.

So you can you could use the already known ':action' symbol,  but if you introduce a new routing component with a symbol name instead of a string,  it would cause havoc.  I don't exactly remember what it did to me, vaguely I remember the routing just didn't work, like my changes to routes.rb were ignored.

I tracked down and read an explanation,  but that was early in my journey into RoR,  and I couldn't fathom the cryptic post.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.