Write a Block Method4:42 with Jason Seifer
Writing a method that takes a block is very similar to writing any other method. In this video, you'll learn to write your own block methods.
Our first block:
def block_method puts "This is the first line in block_method." end block_method do puts "This statement is called from the block." end
This doesn't do anything! Why? We need to tell Ruby what to do with the block. We do that with the
yield keyword which will jump out of the method and execute the block:
def block_method puts "This is the first line in block_method." yield puts "This statement is after the yield keyword." end block_method do puts "This statement is called from the block." end
This will print out:
This is the first line in block_method. This statement is called from the block. This statement is after the yield keyword.
Let's talk for a moment about writing methods that can be written with blocks. 0:00 There's actually nothing special about methods that can be called with blocks. 0:05 Just like with the loop or 0:09 times methods, we can write our own methods that utilize blocks. 0:11 When we do that, the block is considered an argument to the method. 0:16 Let's see how that works now, using Workspaces. 0:21 All right. 0:25 So, let's go ahead and write our first method that takes a block. 0:25 Let's go ahead and create a new file, and we will call this block method.rb. 0:30 And let's go ahead and name our method, block method. 0:37 And we will close the method definition. 0:44 And now, for starters, let's just go ahead and print out, 0:47 this is the first line in block method. 0:51 Now we have to be able to call this. 0:57 So, we call the name of our method, 1:00 which in this case is block method, Now we'll send a block into this. 1:02 And we'll just say. 1:09 This statement is called from the block and we will write end there. 1:12 So here's our method. 1:19 We haven't done anything special to tell it to expect a block. 1:22 And then we're calling the method, sending in a block, and 1:25 then hopefully this code gets executed. 1:30 So let's go ahead and run this and see what happens. 1:33 And all we see is this is the first line in block method. 1:41 Now the reason is, even though we sent in a block, 1:45 we haven't told our method what happens to it, or what to do with the block. 1:48 The way we do that is with a special keyword in Ruby called yield. 1:54 When we put the yield keyword in here, Ruby is going to know 1:59 to come out of the block where the yield statement is, then execute the code 2:02 inside the block which in this case is going to be our put statement. 2:07 Now let's try running this again and see what happens. 2:12 Now when I run it, we can see that it says, 2:17 this is the first line in block method, and then we get the printout that 2:19 we expected which is, this statement is called from the block. 2:23 Now, if we wanted to, we could do something after the yield here, and 2:28 print out, this statement is after the yield keyword. 2:34 Now let me just clear the screen here and run this again. 2:44 This statement is after the yield keyword is printed out right 2:48 after this statement is called from the block. 2:52 So when Ruby executes this, it sees the block method definition, 2:56 and then it sees the method call, and then here is the block. 3:01 You can think of this as one statement or one block of code. 3:05 So Ruby runs this block method, 3:09 encounters this put statement, runs it, gets to the yield. 3:12 Then this entire code block is executed, and 3:18 we return to what comes after it, which is the put statement. 3:22 Now, you might wonder what would happen if we put in another yield keyword. 3:28 See if you can guess before we run this. 3:33 And when we put in that second yield keyword, you can see that, 3:39 this statement is called from the block, is executed twice. 3:43 So each time we see the yield keyword in this method, 3:48 the block of code is executed. 3:50 Let's see what would happen if we just called block method without a block. 3:55 Clear my screen here. 4:01 And let's go back and scroll through the output here. 4:05 We get this is the first line in the block method. 4:10 This statement is called from the block, and this is after the yield keyword. 4:12 Then, block method gets called again, and 4:17 we get to this is the first line in block method, but 4:20 it says no block was given, and we get a local jump error. 4:24 So we can see that this requires a block. 4:30 Try practicing writing some of your own methods now using blocks 4:36 in the yield statement using workspaces. 4:39
You need to sign up for Treehouse in order to download course files.Sign up