gpt4 book ai didi

ruby - 如何解决 Rubocop respond_to_missing?罪行

转载 作者:数据小太阳 更新时间:2023-10-29 07:15:02 28 4
gpt4 key购买 nike

Rubocop 给我以下罪行

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

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

缺少的方法定义为:

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

我试着用下面的代码修复它,从 here 中找到一个例子

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

但现在 Rubocop 给我以下罪行:

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

我似乎无法弄清楚出了什么问题。如您所见,我在 else block 中使用了 super。

最佳答案

Rubocop 期望调用 super 时不带参数。由于您传递给 super 的参数与您收到的参数相同,您可以简单地删除参数:

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

关于ruby - 如何解决 Rubocop respond_to_missing?罪行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38605669/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com