On Jan 16, 2012, at 12:33 PM, Agis A. wrote:

> Hi there people!
> 
> I'm a newbie to both Rails & Ruby, I've just started reading the "Agile 
> Development with Rails" book yesterday.
> 
> I've noticed that in most of the times, we 're calling functions without 
> parentheses like validates :title, presence: true or assert product.invalid? 
> Sometimes though (mostly in views as I've noticed) we're using parentheses, 
> like <%= number_to_currency(product.price, unit: "€").

You'll notice that when you do end up using the parentheses, it's to 
disambiguate nested function calls:

link_to 'foo', @bar, baz => 'blarg'

vs 

link_to 'foo', bar_path(bar), baz => 'blarg'

or more elaborately

link_to( 'foo', bar_path(bar), baz => 'blarg' ) if bar

> 
> My question: is this generally the preferred way to code in Rails? If yes, 
> why? Sorry if this sounds silly but I can't clearly see why we prefer one way 
> over the other.
> 
> Also, I think it's kinda hard to get used to the parentheses-less style, 
> cause everytime I see a line of code I must closely watch it's syntax to 
> determine if this is a function call or something else. Is it only me?

No, it's not just you. Mostly, though, I find that it makes it difficult to 
switch back and forth between Ruby and JavaScript, as I need to do every day. I 
keep making newbie mistakes in JS like leaving the parentheses out of if 
statements.

> 
> Generally, I'm following the rule that every piece of code that has a word 
> and then a space and then something else, for example respond_to :blah, blu: 
> 'test' is a function call, isn't that right?

Seems like a good way to put it.

Walter

> 
> Thanks in advance.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/rubyonrails-talk/-/6acBFxIsxXAJ.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rubyonrails-talk?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to