Search code examples
rubyrubocop

How to omit the outer braces around an implicit options hash in Rubocop?


Rubocop does not swear at code where a hash with parentheses is passed to the method parameter, for example:

user.set({ name: 'John', age: 45, permissions: { read: true } })

I would like him to consider only such lines as correct:

user.set(name: 'John', age: 45, permissions: { read: true })

I found this issue. Does this mean that I will not be able to configure rubocop for this?


Solution

  • Since the keyword arguments changes in ruby 2.7 and 3.0, the 2 are no longer equivalent.

    Sometimes the curly braces are required for the method to understand the arguments correctly.

    Example:

    def foo(x = {}, y: 1)
      puts x.inspect
      puts y.inspect
    end
    
    foo({y: 2}) # {:y=>2}; 1
    foo(y: 2) # {}; 2
    

    This is why the option was removed from Rubocop in the issue that you found and why it is unlikely to be re-added.