ruby-style-guide icon indicating copy to clipboard operation
ruby-style-guide copied to clipboard

Revise "No Braces Options Hash" rule

Open pocke opened this issue 5 years ago • 0 comments

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

pocke avatar Jan 13 '20 13:01 pocke