I know that many ruby style guides insist on parentheses around method arguments for method definitions. And I understand how parentheses are sometimes needed syntactically for method calls.
But can anybody provide of example of why Ruby ever needs parentheses around arguments for a method definition? Basically, I'm looking for a reason besides "it looks better".
If you had neither parentheses nor a semicolon as pointed out in this comment, it would be ambiguous.
def foo a end # => Error because it is amgiguous.
# Is this a method `foo` that takes no argument with the body `a`, or
# Is this a method `foo` that takes an argument `a`?
Also when you want to use composite arguments, you cannot omit the parentheses:
def foo(a, b), c
p [a, b, c]
end
# => Error
def foo((a, b), c)
p [a, b, c]
end
foo([1, 2], 3) # => [1, 2, 3]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With