开发者

Is it appropriate to use parenthesis in a Rails application?

I'm very new to rails but I really like how easy it is to incorporate TDD, a/b testing, MVC, etc... so I have decided to use rails for my next project. One thing I don't like about rails is the style that people use for function calls. The following line is what I would expect to see in a typical rails User controller:

validates :name, :presence => true

I really don't like this style because it makes it hard for a newb like me to understand that validates is a function that takes in two arguments the second of which is a hash. For me, this syntax is much more clear:

validates(:name, {:presence => true})

Now, I want this project to grow so I will be brining in other Rails devs who are more skilled than myself. So, the question is, if I开发者_JS百科 write this app in my style, will it be very frustrating for people who are used to the traditional rails way or can I go ahead and use parenthesis and curly braces to make the structure more explicit?


They will probably try to vote you off the island...

Try to adapt to the Ruby conventions. See the valuable thread here:

http://railsforum.com/viewtopic.php?id=1042

"If I could offer only one piece of advice to you, it would be this: follow the Rails conventions"

Check out the unofficial conventions (written for a google dept.)

http://www.caliban.org/ruby/rubyguide.shtml


There is no real canonical style guide for Ruby so you can go ahead and do that. And yes there will be devs that will be frustrated with code like that (especially ruby veterans).

A lot of Rails code tries to look like natural language on purpose and people will argue that "programmifying" it kind of defeats that purpose.

However I believe it's a minor point - as long as you write good code - good devs will accommodate any style.


Ruby doesn't require parenthesis unless not having them would make the code ambiguous. To borrow from the Ruby Koans

  # (NOTE: We are Using eval below because the example code is
  # considered to be syntactically invalid).
  def test_sometimes_missing_parentheses_are_ambiguous
    eval "assert_equal 5, my_global_method 2, 3" # ENABLE CHECK
    #
    # Ruby doesn't know if you mean:
    #
    #   assert_equal(5, my_global_method(2), 3)
    # or
    #   assert_equal(5, my_global_method(2, 3))
    #
    # Rewrite the eval string to continue.
    #
  end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜