B.1 No parentheses around arguments
When we call a method that has arguments, we put the arguments within parentheses (()
) immediately next to the method name (with no space between the method name and the opening parenthesis):
"hello".gsub("l", "z")
# => "hezzo"
Ruby will allow you to, optionally, drop the parentheses around arguments:
"hello".gsub "l", "z"
# => "hezzo"
It’s important to remember not to mix these two by putting a space before parentheses!
# If you're going to use parens, don't also have a space
"hello".gsub ("l", "z") # bad
I like to include the parentheses around arguments because it makes it clear what’s what, especially when you are chaining multiple methods together. The only time that I drop the parentheses is if I am using trivial methods at the beginning of a line, like p
or ap
:
# Rather than
"Hi there")
p(
# I will usually do this instead:
"Hi there" p