Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 堆栈级别太深,但不确定是什么导致了无限递归_Ruby - Fatal编程技术网

Ruby 堆栈级别太深,但不确定是什么导致了无限递归

Ruby 堆栈级别太深,但不确定是什么导致了无限递归,ruby,Ruby,所以我对它的研究似乎表明,这里有某种东西导致了无限递归,但我不确定它是什么。有人能指出我做错了什么吗 def initialize(_val) @start_value = _val end def method_missing(method, *args) if method.starts_with?("plus") then num = method[4 .. method.length] if (/^[\d]+(\.[\d]+){0,1}$

所以我对它的研究似乎表明,这里有某种东西导致了无限递归,但我不确定它是什么。有人能指出我做错了什么吗

def initialize(_val)
    @start_value = _val
end

def method_missing(method, *args)
    if method.starts_with?("plus") then
        num = method[4 .. method.length]
        if (/^[\d]+(\.[\d]+){0,1}$/ === num) then
            number = Integer(num)
            self.class_eval("def #{method}; return @start_value + x; end")
        self.plus(number)
        else
            super.method_missing
        end
    else
        super.method_missing
    end
end

end

最明显的解释是,如果没有将
plus
定义为实例方法。但是为什么不添加一个
将方法
放在你的
方法(缺少
方法)的顶部,这样你就可以看到发生了什么?

你不应该这样做:

super.method_missing
你想要这个:

super

在这两种情况下,您都将使用不带参数的
super
,这是调用方法的祖先版本的正确方法,在本例中
method\u missing
。但是在你的版本中,你会对结果重复调用
method\u missing
,这就是它得到无穷大的原因。

这很奇怪。我希望您的代码立即抛出一个NoMethodError,用于调用符号上的
以?
开头。另外,您使用的
super
错误-应该是
super
。通过添加
.method\u missing
,调用超类的
method\u missing
的结果就是调用
method\u missing