4 ms·
the convention that i think most rubyists gravitate toward is to always include parens for method calls when calling something that takes a parameter. omit if t
by weaksauce 3y ago
the convention that i think most rubyists gravitate toward is to always include parens for method calls when calling something that takes a parameter. omit if there are no parameters.
- sodapopcan 3y agoIt is, and it's how I do it when I write Ruby. I was just responding to "it doesn't matter" part of the parent comment. If you decide not to use parens, then you get situations like the one I illustrated where you have to use parens for chaining then you're left with a decision about what to do with the last one.
- andrewmutz 3y agoWhy are the parens important in this example?
- sodapopcan 3y agoYou can't chain without parens. # Does not work "foo bar" .split " " .join " " There was a proposal to introduce a "pipe" operator at one point which was shot down [0]. It wasn't actually a pipe operator at all and really just an operator that would have made the previous syntax possible (|>, instead of .) [0] https://dev.to/baweaver/ruby-2-7-the-pipeline-operator-1b2d https://dev.to/baweaver/ruby-2-7-the-pipeline-operator-1b2d
- epidemian 3y agoYes, definitely omit parentheses when calling a method without arguments. For calls with some non-block arguments, it's not so clear-cut as "always include parentheses". Some methods are so prevalent that they almost become part of the language "syntax", even though there's nothing syntactically special about them. For example, you rarely see parentheses used on calls to `attr_accessor` or `private`: class Cat attr_accessor :fluffiness private def plan_world_domination # TODO end end I think parentheses around `private(def some_method ... end)` would be especially non-idiomatic. Even though passing the result of the `def` expression to the `private` method is exactly what's happening. Another place where it's usual to omit method call parentheses is on embedded DSLs. For example, in RSpec it's uncommon to include parentheses on calls to `describe` or `it`. describe "RSpec DSL" do it "looks neater without parentheses" do # ... end end
- weaksauce 3y agoyeah there will always be edge cases but those are few and why i hedged with gravitate toward. private feels like a part of the language actually like an if statement instead of a method though.