ruby-style-guide
ruby-style-guide copied to clipboard
Conflict in a DSL style
# bad
expect(bowling.score).to eq 0
# good
expect(bowling.score).to eq(0)
But:
Omit parentheses around parameters for methods that are part of an internal DSL (e.g. Rake, Rails,
RSpec)
IMO eq 0
looks better
+1
I would leave parentheses only for cases where it doesn't work without them ( e.g. expect(hash).to eq({})
)
The comments on the example should be updated because they contradict the heading.
"Only omit parentheses for...Methods that are part of an internal DSL (e.g., Rake, Rails, RSpec)".
# good. it's part of rspec's internal DSL
expect(bowling.score).to eq 0
If you have to pass in an array or hash then use parenthesis. This way the example doesn't contradict the rule.
Only omit parentheses for...Methods that are part of an internal DSL (e.g., Rake, Rails, RSpec)
I'd relax this rule to "Always use parentheses for ... methods that are NOT part of an internal DSL".
expect(bowling.score)
.to be > 0
.and be_odd # BOOM
A less contrived example:
expect([1, 2, 3]) # Omiting those parentheses with go BOOM
.to include(be_odd, be_even) # Those as well
.and have_attributes(size: 3) # And those too
Yeah, that's a fair point. I guess to me it was obvious you shouldn't omit mandatory parentheses. :D And, of course, not all DSLs are the same and rely heavily on omitting parens.