Matt
Matt

Reputation: 7249

Recursion in yield

So i am trying to do something like this:

def func(x,y)
     if x.length == 1 then
         n = x.pop()
         yield(n,y)
     else
         n = x.pop()
         yield(n,func(x,y))
     end
end

calling it like:

a = func([1,2,3,4,5],0) do |x,y|
    x+y
end

Is it possible to do something like this? I keep getting no block given (yield) (LocalJumpError).

I even tried doing something a little different:

def func(x,y)
    func(x,y) do |tail|
        ..
    end
end

but no luck

Thanks.

Upvotes: 10

Views: 4444

Answers (2)

Hemanth
Hemanth

Reputation: 5145

You are missing to pass the block in the recursive call.
The recursive call should be like as below:-

yield(n,func(x,y)) { |x,y| x+y})

Since you missed to pass the block in the recursive call, when the code hits:-

     if x.length == 1 then
     n = x.pop()
     yield(n,y) <<<< Here 

the method func doesn't have block passed as argument,in the recursive call, but ruby tries to call a non-existent block and hence the error.

Upvotes: 2

Paige Ruten
Paige Ruten

Reputation: 176743

Yes, you can take the block as an argument explicitly:

def func(x, y, &block)

You can still yield to it with the yield keyword, but you can also pass it as you recurse:

yield(n, func(x, y, &block))

The & in both cases means that the block argument is not a normal argument, but represents the block that can be attached to any Ruby method call.

Upvotes: 13

Related Questions