How to solve Rubocop response_to_missing? the crime

Rubocop gives me the following violation

lib/daru/vector.rb:1182:5: C: Style/MethodMissing: When using method_missing, define respond_to_missing? and fall back on super.
    def method_missing(name, *args, &block) ...
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

An invalid method is defined as:

def method_missing(name, *args, &block)
  if name =~ /(.+)\=/
    self[$1.to_sym] = args[0]
  elsif has_index?(name)
    self[name]
  else
    super(name, *args, &block)
  end
end

I tried to fix this with the code below, in which you can see an example here

def respond_to_missing?(method_name, include_private=false)
  (name =~ /(.+)\=/) || has_index?(name) || super
end

But now Rubocop gives me the following violation:

lib/daru/vector.rb:1182:5: C: Style/MethodMissing: When using method_missing, fall back on super.
    def method_missing(name, *args, &block) ...
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I can’t understand what happened. As you can see, I return to super in the else block.

+4
source share
1 answer

Rubocop expects to superbe called without arguments. Since the arguments you pass in superare the same as you, you can simply remove the arguments:

def method_missing(name, *args, &block)
  if name =~ /(.+)\=/
    self[$1.to_sym] = args[0]
  elsif has_index?(name)
    self[name]
  else
    super
  end
end
+6
source

Source: https://habr.com/ru/post/1649245/


All Articles