ruby-style-guide
ruby-style-guide copied to clipboard
Revise "No Braces Options Hash" rule
https://github.com/rubocop-hq/ruby-style-guide#no-braces-opts-hash
See also https://github.com/rubocop-hq/rubocop/issues/7641
foo(kw: 1) and foo({kw: 1}) will have different meanings in Ruby 3. And Ruby warns them since Ruby 2.7 if it is used unexpectedly, and Ruby raises an argument error in Ruby 3.
But the style guide says "always omit the curly braces".
So I propose revising the guide. I think the guide should mention both cases, Hash and keyword arguments.
For example:
def foo(a:, b:)
p a, b
end
def bar(hash)
p hash
end
# good
foo a: 1, b: 2
# good
bar({a: 1, b: 2})
# bad - foo expects keyword arguments, but it passes a Hash
foo({a: 1, b: 2})
# bad - bar expects a hash, but it passes keyword arguments
bar a: 1, b: 2