Calling functions without parentheses. When?

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: “€”).

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?

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?

Thanks in advance.

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

Being able to elide the parentheses is a "convenience". But unfortunately, sometimes then more complex calls don't evaluate in the right order, and require you to use parentheses to disambiguate, which means you really can't go completely without, so there's only 2 styles possible:

- mixed, sometimes indicating function calls with parentheses and sometimes not
- always use parentheses

I expect that when I put it like that, you'll be able to guess my preference :wink:

You cleared it for me, thank you. I don’t understand your code thought :stuck_out_tongue: