Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve Rubocop respond_to_missing? offence

Tags:

ruby

rubocop

Rubocop gives me the following offence

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) ...
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

The method missing 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 fixing it with the below code sighting an example from here

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

But now Rubocop give me the follow offence:

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 seem to figure out what's wrong. As you can see I'm falling back on super in the else block.

like image 757
Lokesh Avatar asked Jul 27 '16 06:07

Lokesh


1 Answers

Rubocop expects super to be called without arguments. As the arguments you are passing to super are the same as those you received, 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
like image 182
Justin Ko Avatar answered Oct 30 '22 02:10

Justin Ko