Alex Alifimoff
Alex Alifimoff

Reputation: 1849

Stack Level Too Deep, but unsure what is exactly causing the infinite recursion

So my research on this seems to indicate that the there is something in here that is causing infinite recursion, but I'm not sure waht it is. Can anyone point out what I'm doing wrong?

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

Upvotes: 0

Views: 218

Answers (2)

Rob Davis
Rob Davis

Reputation: 15772

You shouldn't do this:

super.method_missing

You want this:

super

In both cases you'd be using super with no args, which is the proper way to call an ancestor's version of a method, in this case method_missing. But in your version you're then calling method_missing redundantly on the result, which is where it gets all infinitey.

Upvotes: 1

Ben Taitelbaum
Ben Taitelbaum

Reputation: 7403

The most obvious explanation would be if you don't have plus defined as an instance method. But why not add a puts method to the top of your method_missing method so you can see what's going on?

Upvotes: 1

Related Questions