Call the method from within to execute it again

How in ruby ​​can I call the method from the inside to restart it. In the example below, when @dest_reenter is yes, I would like the b_stage method to run again

def b_stage if @dest_reenter == 'yes' @dest_reenter = nil b_stage end end 
+4
source share
2 answers

This is how you do recursion, but using these instance variables is not the way to go. A better example might be the following:

 def b_stage(i) if i < 5 puts i i += 1 b_stage(i) end end 

If you call b_stage(0) , the output will be

 0 1 2 3 4 
+6
source

Use a separate method:

 def go ... middle_thing(true) end def middle_thing(first_time) next_page unless first_time == true parse_page end def parse_page ...(parsing code) middle_thing(false) end 
0
source

All Articles