Displaying a Menu3:57 with Jason Seifer
Our users will need a way to interact with our program. In this video, we begin to offer that functionality by displaying a simple menu and waiting for input.
def run loop do puts "Address Book" puts "e: Exit" print 'Enter your choice: ' input = gets.chomp case input when 'e' break end end end
We call this by saying:
[MUSIC] 0:00 Now that we have most of our classes set up, we can work on input and output. 0:04 We're going to use a class from the Ruby Standard Library to save things between 0:09 sessions. 0:13 This is a lot easier than you think. 0:15 Let's go ahead and see how that works now, using WorkSpaces. 0:17 Okay, so we have our functionality with our address book working so far. 0:21 We can add contacts. 0:26 We can add phone numbers and addresses to our contacts. 0:27 And then, we can go ahead and search through it if we want to. 0:31 So now, we're going to make this more of a command line application. 0:35 So what that means is we're going to have a menu 0:40 allowing our user to select something and 0:44 then work with it in the command line, rather than from inside of a Ruby file. 0:49 Now the way that we're gonna do this is create a method that loops through, 0:54 displays a menu and then will call each of these methods, 0:59 depending on what the user enters. 1:03 So lets go ahead and create that. 1:07 Now, before we do that, let's go ahead and look at all this stuff down here. 1:09 We're gonna just go ahead and get it out of there. 1:12 So we have this address book that we initialize. 1:16 And let's make a method called run which will display our menu on a loop. 1:20 And that's all that we're gonna do to call 1:29 this run method which will display our menu. 1:33 So now let's go ahead and write that. 1:38 Here is our run method. 1:40 Now we're gonna use a loop, to display our menu. 1:47 And we are going to need a way to exit the loop. 1:52 But, we'll write that in just a second. 1:57 So for right now let's just display address book. 1:58 And let's go ahead and give an option here. 2:03 If somebody enters the letter E they can exit. 2:06 So we've got this little menu here. 2:11 This is just the start of it. 2:13 And then let's go ahead and ask for their input. 2:15 We'll say, Enter your choice, 2:20 and then we will grab the input and assign it to a variable. 2:26 We'll use the getString method, which we'll just grab it from standard input. 2:30 And that will go in with a new line. 2:35 So we have to use the chomp method to get just the character 2:38 that we're interested in. 2:41 And then let's go ahead and just make that lower case so that when we search for 2:43 it we know what's going on. 2:47 Okay, now we know we're gonna add on to this later, so 2:49 we're going to use a case statement right now. 2:53 Since all we have is one option, we're just gonna have one single option 2:55 in this case statement, so when the user types 3:00 the letter E, we're gonna break out of this loop. 3:08 Now let's go ahead and ruin this and see what happens. 3:13 Address book, okay, let's type the letter e and make sure it exits. 3:20 Uh-oh. 3:25 Undefined method input for address book. 3:26 I forgot an equals sign. 3:29 Okay, let's clear the screen and run that again. 3:32 Okay. Exiting works just fine. 3:36 Let me clear the screen. 3:38 We'll run this again. 3:40 If I type a, okay we just get this menu again. 3:43 DE2, whatever. 3:46 Okay. Good. 3:49 Now we know that E exits. 3:51 This is a good start for our menu so we know that it's working. 3:53
You need to sign up for Treehouse in order to download course files.Sign up