- Understand how the
break
keyword is used in looping. - Understand how the
gets
method is used to store a user's input. - Utilize both
break
andgets
in your own program.
What happens when we have an infinite loop that keeps on going forever? What happens when, given a specific output, we want to break out of that loop? This is where the break
keyword comes in.
Let's take a look at the following example:
counter = 1
while counter <= 5
puts "The counter is at: #{counter}"
break if counter == 3
counter = counter + 1
end
puts "We're done looping!"
We've added a break
keyword in the while
loop. When the condition if counter == 3
is met, it will break out of the loop completely, and it will not continue to iterate all the way up to 5
. What's going to be the output?
"The counter is at: 1"
"The counter is at: 2"
"The counter is at: 3"
"We're done looping!"
Let's say we are writing a program in order to annoy someone (maybe a younger sibling or beloved coworker?) Our program should puts
the phrase "What's up, Doc?" again and again and again until our annoyed user finally inputs "STOP" into the terminal. In this case, we are looping indefinitely and breaking if a user inputs "STOP".
After we discuss when NOT to use the break
keyword, we will show you how to get and use a user's input.
The use of the break
keyword is generally avoided. In upcoming lessons, we'll learn how to write more elegant, self-explanatory loops that don't require break
-ing. It is important to understand how break
works, however, and it is useful in command line applications.
The gets
method accepts a single line of data from the standard input - the keyboard in this case. Then, your program can assign that value to a variable.
Note On Standard Input: The standard input is a default stream supplied by many operating systems that relates to the standard way to accept input from the user. In our case, the standard input is the keyboard. For now, just think of the standard input as what our user types into the terminal.
We're going to play with method greeting
that uses puts
to ask a user for their name. Then, their response that they type into the terminal will be stored in a variable via the gets
method. Then, the greeting
method will proceed to puts
a greeting: "Your name is < name >"
Copy and paste the following into IRB:
def greeting
puts "Please type your name:"
name = gets
puts "Your name is #{name}!"
end
greeting
The gets
method, used without .chomp
, appends a new line to the data you are using it to "get". We don't want a new line! If we preserved our new line by failing to use .chomp
, the final output of the greeting
method would look like this:
"Your name is <name>
!"
The .chomp
method removes that new line for us.
Now we'll return to our previous example using break
and gets
.
Let's think about our example from earlier. We want a program that continuously outputs "Whats up, Doc?" until the user can't take it anymore and finally inputs "STOP". In other words, we need to create a loop that will continue to ask the user "What's up, Doc?", but will break out of the loop when the user types in "STOP".
This will require us to get some user input and to break the loop accordingly.
Check out the following method:
def annoying
loop do
puts "What's up, Doc?"
answer = gets.chomp
break if answer == "STOP"
end
puts "Okay, okay, jeez. I'll stop. Sorry."
end
annoying
If you type the above code into IRB (hint: do it), you will probably be annoyed. Your interaction should go something like this:
What's up, Doc?
nothing
What's up, Doc?
I said nothing
What's up, Doc?
nothing!
What's up, Doc?
ugh
What's up, Doc?
quit it
What's up, Doc?
stop it
What's up, Doc?
stop
What's up, Doc?
stop
What's up, Doc?
STOP
Okay, okay, jeez. I'll stop. Sorry.
=> nil
Let's break this down. We have a method, called annoying
, that contains a loop. The loop says:
puts
the phrase "What's up, Doc?"- Collect the user's input via the
gets.chomp
method and store them in a variable calledanswer
. - Check to see if the
answer
variable points to a string value of"STOP"
. - If it doesn't, go back to the top of the loop and repeat. If it does,
break
out of the loop and proceed to the next line of code:puts "Okay, okay, jeez. I'll stop. Sorry."
Fork and clone this lab. Read the below explanation and run the test suite to get started.
- Okay, now that we've mastered feather levitation, we're ready for the levitation quiz! Fill out the content of the method
levitation_quiz
so that it contains a loop that asks the user "What is the spell that enacts levitation?" It should then store the answer in a variable calledanswer
using thegets.chomp
method. The loop shouldbreak
if the user's answer is "Wingardium Leviosa". When the loop breaks, our method shouldputs
"You passed the quiz!" Otherwise, the loop should continue. - Important: The Learn gem cannot interact with
gets.chomp
in the command line. To get your tests passing when you runlearn
, make sure you are not calling the method you define anywhere inlevitation_quiz.rb
. You can call the method and run the program withruby levitation_quiz.rb
to play with your program and see it working, but to run the tests withlearn
, comment out or delete the line where you call the method.
View break and gets on Learn.co and start learning to code for free.