What is the proper way in ruby to call a method from within itself to rerun In the sample below when @dest_reenter is equal to yes I would want the b_stage method to execute again
def b_stage
if @dest_reenter == 'yes'
@dest_reenter = nil
b_stage
end
end
Calling a function inside of itself is called recursion. It's a technique used for many applications, like in printing out the fibonacci series.
Yes you can. It is called recursion .
No, it is perfectly fine to call method from itself - the name is "recursion" / "recursive function".
That is how you do recursion, but using those instance variables isn't the way to go. A better example would be something like this:
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With