Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to end execution of a block in Ruby?

Tags:

return

ruby

block

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?

like image 657
HappyDeveloper Avatar asked Mar 20 '12 16:03

HappyDeveloper


People also ask

How do you exit a block in Ruby?

To break out from a ruby block simply use return keyword return if value.

How do you break a method in Ruby?

Exiting A Method Ruby methods end naturally on their last line of code. Use the return keyword.

What is start and end in Ruby?

Every Ruby source file can run as the BEGIN blocks when the file is being loaded and runs the END blocks after the program has finished executing.

How do you exit a loop in rails?

In Ruby, we use a break statement to break the execution of the loop in the program. It is mostly used in while loop, where value is printed till the condition, is true, then break statement terminates the loop. In examples, break statement used with if statement. By using break statement the execution will be stopped.


1 Answers

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

like image 172
HappyDeveloper Avatar answered Oct 04 '22 05:10

HappyDeveloper