Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break out from a ruby block?

Tags:

ruby

Here is Bar#do_things:

class Bar      def do_things     Foo.some_method(x) do |x|       y = x.do_something       return y_is_bad if y.bad? # how do i tell it to stop and return do_things?        y.do_something_else     end     keep_doing_more_things   end end 

And here is Foo#some_method:

class Foo   def self.some_method(targets, &block)     targets.each do |target|       begin         r = yield(target)       rescue          failed << target       end     end   end end 

I thought about using raise, but I am trying to make it generic, so I don't want to put anything any specific in Foo.

like image 210
user169930 Avatar asked Sep 10 '09 00:09

user169930


People also ask

How do you break in Ruby?

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.

What is next if in Ruby?

next has two meanings in Ruby, one as a keyword, the other as a method. As a keyword next in Ruby generally refers to the next statement for iteration blocks. It specifically affects the loop style enumerator blocks while, for, until, etc.

How do you create a loop in Ruby?

The simplest way to create a loop in Ruby is using the loop method. loop takes a block, which is denoted by { ... } or do ... end . A loop will execute any code within the block (again, that's just between the {} or do ...


2 Answers

Use the keyword next. If you do not want to continue to the next item, use break.

When next is used within a block, it causes the block to exit immediately, returning control to the iterator method, which may then begin a new iteration by invoking the block again:

f.each do |line|              # Iterate over the lines in file f   next if line[0,1] == "#"    # If this line is a comment, go to the next   puts eval(line) end 

When used in a block, break transfers control out of the block, out of the iterator that invoked the block, and to the first expression following the invocation of the iterator:

f.each do |line|             # Iterate over the lines in file f   break if line == "quit\n"  # If this break statement is executed...   puts eval(line) end puts "Good bye"              # ...then control is transferred here 

And finally, the usage of return in a block:

return always causes the enclosing method to return, regardless of how deeply nested within blocks it is (except in the case of lambdas):

def find(array, target)   array.each_with_index do |element,index|     return index if (element == target)  # return from find   end   nil  # If we didn't find the element, return nil end 
like image 152
JRL Avatar answered Sep 28 '22 09:09

JRL


I wanted to just be able to break out of a block - sort of like a forward goto, not really related to a loop. In fact, I want to break of of a block that is in a loop without terminating the loop. To do that, I made the block a one-iteration loop:

for b in 1..2 do     puts b     begin         puts 'want this to run'         break         puts 'but not this'     end while false     puts 'also want this to run' end 

Hope this helps the next googler that lands here based on the subject line.

like image 35
Don Law Avatar answered Sep 28 '22 11:09

Don Law