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.

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Walter Lee Davis at Jan 16, 2012 at 6:05 pm

    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Agis A. at Jan 16, 2012 at 9:21 pm
    You cleared it for me, thank you. I don't understand your code thought :P

    --
    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/-/j6x0JTnXawYJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Scott Ribe at Jan 16, 2012 at 6:40 pm

    On Jan 16, 2012, at 10:33 AM, Agis A. wrote:

    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.
    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 ;-)

    --
    Scott Ribe
    scott_ribe@elevated-dev.com
    http://www.elevated-dev.com/
    (303) 722-0567 voice




    --
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 16, '12 at 5:33p
activeJan 16, '12 at 9:21p
posts4
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase