Ruby Loops5:19 with Jason Seifer
A loop is a piece of code that will run the statements inside until some condition is met. We can use loops to do things like asking a user if they want to keep entering input until they type "done" or "no" or something similar. There are several different kinds of loops available in Ruby. We can write a loop using the "loop" keyword.
We create an infinite loop when writing code in this lesson. To exit the loop, hold the
Ctrl key and press
C. On Windows, you can also use
Ctrl + Pause/Break.
Infinite Loop: A loop that never exits.
Here is our simple loop:
loop do print "Do you want to continue? (y/n) " answer = gets.chomp.downcase end
The above code creates an infinite loop, or, a loop that never exits. To fix that, we can use the
break keyword to exit the loop.
loop do print "Do you want to continue? (y/n) " answer = gets.chomp.downcase if answer == "n" break end end
[MUSIC] 0:00 Hi I'm Jason, a Ruby developer. 0:04 In this course we're going to look at loops and iteration in Ruby. 0:07 So far when we've been writing our programs and 0:11 want to do something more than one time we've written those things out. 0:14 In the Ruby Collections course, when we were adding items to our grocery list, we 0:19 wrote a method called add list item, which added a new item to our grocery list. 0:24 But this tedious and computers are really good at doing things for us. 0:30 Another way we could have written that program is using something called a Loop. 0:36 A loop is a piece of code that will run the statements inside 0:41 until some condition is met. 0:45 Using the grocery list example, we could have a loop ask a user if they wanna keep 0:48 entering grocery list items until they type done, or no, or something similar. 0:53 There are several different kinds of loops available in Ruby. 1:00 We can write a loop using the loop keyword. 1:04 Let's see how that works now using workspaces. 1:08 Okay, so I have just launched a new Ruby workspace. 1:12 If you see the welcome document, you can go ahead and close it. 1:16 Now let's go ahead and create a new file. 1:21 And we'll call this loop.rb. 1:25 In order to create a loop, we use the loop keyword followed by the word do. 1:30 And then, we close this, by typing end. 1:39 Now, whatever is inside, what is called a block here, 1:44 between the do and end, will be run, until we tell it to stop. 1:49 So let's go ahead and just print something out here. 1:55 We'll just say, do you want to continue? 2:01 And then we'll get the answer. 2:07 Now I've saved the file and let's click down here 2:13 into the console area and run this by typing ruby loop.rb. 2:18 Now we see this asks do we want to continue. 2:27 And I will type y. 2:30 Actually, we can type anything we want here. 2:33 And this is going to continue forever. 2:36 Because we haven't told it to do anything, or given it any way to exit the loop. 2:40 We can stop this loop by holding the Ctrl+C. 2:45 That will give us this message saying that it has been interrupted. 2:50 Don't worry about that. 2:56 Ctrl+C is going to cancel the currently running program, or 2:57 in this case, interrupt it. 3:02 You'll notice that this loop goes on forever. 3:05 This is something called an Infinite Loop. 3:08 An infinite loop occurs when there's no condition to exit the loop, 3:12 or the condition that would exit the loop can never be met. 3:17 Let's modify the loops so that it has a way to exit. 3:22 When we look back up here, we see loop, and then do, and end. 3:25 Do and end is something called a block in Ruby. 3:31 Blocks are written one of two ways, using either the do and end keywords. 3:35 You can also use curly braces. 3:41 And if we run this again, we should see the same thing. 3:47 I'm gonna clear my screen here by holding the Ctrl+L. 3:50 And we see if we continue, we get the same thing. 3:59 And once again, I'm going to press Ctrl+C to exit. 4:04 And I'm going to change this back from curly braces to do and end. 4:09 Blocks can be written either way, with curly braces or 4:16 with the do and end key words. 4:19 By convention, most Ruby programmers will use do and 4:22 end if the statements inside the block take up more than one line of code. 4:25 If it only takes up one line of code, it's convention to use the curly braces. 4:30 So let's go ahead and fix this infinite loop here. 4:37 And we'll actually do something with the answer. 4:40 We'll say if the answer is a lower case 4:43 n, then we need to exit this loop somehow. 4:49 We can do that using the break keyword, 4:56 which will break us out of the current loop. 4:59 I'm gonna run this again. 5:04 Let me clear my screen here. 5:05 And if I type ruby loop.rb. 5:07 Do you want to continue? 5:10 Yes. 5:11 Do you want to continue? 5:12 No. 5:14 And that exits the loop and also the program. 5:15
You need to sign up for Treehouse in order to download course files.Sign up