HappyDeveloper
HappyDeveloper

Reputation: 12805

How to end execution of a block in Ruby?

I thought blocks were like anonymous functions.

But when I tried to end the execution of a block using return keyword, I think it triggered a return in the scope in which the block was defined in. Is that how they work?

If so, how can I end the execution of a block, in a way that does not trigger a return in the outer scope?

Upvotes: 35

Views: 8855

Answers (2)

HappyDeveloper
HappyDeveloper

Reputation: 12805

The keyword is next, not break. Break seems to be specific for the each method.

Upvotes: 57

rogeriopvl
rogeriopvl

Reputation: 54056

To exit a block or loop use the break keyword.

return will exit a method.

Upvotes: -10

Related Questions