Infinite loops in Ruby
Before we get into infinite loops, lets discuss what infinity is. Infinity is a concept describing something without any bound or larger than any natural number (Wikipedia). Now what does that mean? Lets say we have a function y = 1/x. We can say that y is inversely promotional to ‘x’. As the value of ‘x’ increases, the value of ‘y’ decreases. But what happens to ‘y’ if we let ‘x’ increase and increase without a limit? ‘y’ becomes infinitely small. It will get closer and closer to value of 0 but will never really reach it. So, you can think of infinity as being never ending, just like our function.
Infinite loops in ruby has the same concept as our function from earlier, it is never ending. Without an escape method/conditional, our loops would run indefinitely, and possibly break our applications.
#while true
# puts "oh no i am stuck"
#end
--------------------------------------------------------------------#loop do
# puts "oh no i am stuck"
#end
--------------------------------------------------------------------#until false
# puts "oh no i am stuck"
#end
--------------------------------------------------------------------
All the code above will place you in an infinite loop. It will print “oh no i am stuck” to the screen indefinitely. If you do end up running the above codes in your terminal, use ‘ctrl + c’ to stop and exit the loop.
To this point, I think I have made you, the reader, believe infinite loops are to be avoided and make it seem as if it is a bad. Infinite loops are not always bad. Lets say in our hypothetical gaming website, we had a method called #update_active_users that collects shows the current number of users online. If we were to create a simple loop to continuously update the screen we can create a simple loop such as:
#loop do
# update_active_users
#endThis is great. It will continuously run #update_active_users. But, it might be a little illogical to update it however fast a “loop do” runs. (*Maybe I’ll do a blog post about the speeds of different loops) Anyways, it might be better to update the active users every minute so we can implement a sleep method to our simple loop:
#loop do
# update_active_users
# sleep(60)
#endThis makes a little more sense, let the current loop sleep for 60 seconds before running again. Our loop is still going to run indefinitely but now in increments of 60seconds. MIND = BLOWN
Use this new power and knowledge of infinite loops accordingly(do not let the power get to your head). But if you do and find yourself in an infinite loops, here are few ways to break out of them.
- Set a conditional such as. Only run a loop while a conditional is true or false.
log_in = truewhile log_in == true
puts "hello user"
log_in = false
end
In the above example, our infinite loop will run only once. Well I guess it is no longer an infinite loop. Re-assigning false to the log_in variable falsifies out while statement, breaking the loop.
Inside of our loop, we are re-assigning a new value to our user_input. The line #break if user_input == “I am crying let me go” checks to see if the user_input is equal to “I am crying let me go”. If the user inputs “I am crying let me go”, the loop will break and rest of the code will run.
2. Have a #break or #return statements inside of the loop. Break will break out of the current loop and continue the rest of the code outside of the loop. While #return will return a value to the original caller of the method without running rest of the method.
puts "Are you ready to run around in my loop?"user_input = gets.chompputs "Don't matter to me, but what do you wanna do?"loop do
user_input = gets.chomp
break if user_input == "I am crying let me go"
puts "Still don't care. Keep telling me like I care. What do you wanna do?"endputs "Fine, cry baby I was just playing around."
Inside of our loop, we are re-assigning a new value to our user_input. The line #break if user_input == “I am crying let me go” checks to see if the user_input is equal to “I am crying let me go”. If the user inputs “I am crying let me go”, the loop will break and rest of the code will run.
Loops are great and very useful. I hope my blogpost solidified your understanding of infinite loops, possible uses and how to have an exit clause.