Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Ruby gives us different ways to call blocks. In this video, we'll explore the yield
keyword and using block.call
.
Code Samples
This is an example of calling the block using the call
method:
def get_name(prompt, &block)
print prompt
name = gets.chomp
block.call(name)
name
end
my_name = get_name do |your_name|
puts "That's a cool name, #{your_name}!"
end
puts "my_name: #{my_name}"
The block_given?
keyword can be used to conditionally call a block if one was passed to a method:
def get_name(prompt, &block)
print prompt
name = gets.chomp
block.call(name) if block_given?
name
end
my_name = get_name("Name: ") do |your_name|
puts "That's a cool name, #{your_name}!"
end
puts "my_name: #{my_name}"
Ruby gives us several ways to work
with blocks when we write our methods.
0:01
During your career as a Ruby programmer,
0:05
you'll encounter different ways
methods are written using blocks.
0:07
Let's take a look at some of
the ways blocks can be written now
0:12
using WorkSpaces.
0:15
Okay, so let's take a look at a couple
different ways of calling blocks.
0:17
Now what I'm going to do is open
up this block_argument.rb file
0:21
that we just used and I'm gonna
copy it and then create a new file.
0:27
We'll call it calling_blocks.rb and
0:33
I'm just gonna paste everything from
the block arguments file into there.
0:38
Okay, so here is our get_name method.
0:43
So let's say we wanted to turn this
into a more typical method and
0:48
send an argument where we want to
print the prompt that we send it.
0:54
So we'll say print prompt and
then a colon a space.
1:01
Now when we call it we can say enter your
name and then let's go ahead and run it,
1:11
just make sure it works.
1:15
Okay, so that works just like we expect.
1:20
Now you'll notice we send in the argument
to the method just like we would
1:24
with any other method call.
1:29
And then additionally we
can send in the block.
1:33
Now there's another way that we
can call the block here, but
1:38
it involves assigning
the block to a variable.
1:41
And we do that by using an ampersand and
then assigning it to a variable.
1:46
In this case, we're calling
the block that we send in, block.
1:52
Now, when we do that, I mean,
we could save this, and run it again,
1:57
let me just go ahead and clear the screen.
2:02
We can see that it still works.
2:03
Instead of seeing yield name,
when we assign a block to a variable,
2:09
we can explicitly call it
by saying block.call and
2:15
passing in the argument to it.
2:23
Which in this case is the name.
2:26
So if I clear the screen and run it again,
we can see we get the exact same behavior.
2:29
Now if the block didn't take an argument,
in this case, its name, and
2:37
we're passing it into the block as
your_name, we could just use block.call.
2:41
And if we had another variable in here,
2:48
for example, age.
2:53
If we had two variables that we wanted to
send in or two arguments that we wanted to
3:04
send in to the block,
we could do that this way.
3:08
Let's just clear the screen and
run this again.
3:21
And that is how we would send in
multiple arguments to the block.
3:27
We could do this with
the yield statement as well.
3:34
Yield name and age.
3:37
And clear the screen,
just make sure it runs okay.
3:40
Okay.
3:46
So there we go.
3:47
That's the same thing.
3:47
Now one other interesting thing that we
get right here is another keyword which
3:49
lets us tell whether or
not a block was sent in to a method.
3:56
And that is called block_given
ith a question mark.
4:02
So if we send in a block we can say,
yield the name and
4:13
the age if there is a block and
the just return the name.
4:16
Let's just go ahead and
make sure that works.
4:19
So right now we're still
sending in a block.
4:22
We should see the same thing, and we do.
4:24
But if we were to comment this out,
let's go ahead and
4:30
clear the screen and run it again.
4:35
And we can see it doesn't even run that.
4:36
If we wanted to,
we could get even more fancy and
4:44
say, if there's a block given, well then
we do all the assignment and the yield.
4:51
And if there is no block given,
we'll say no block given.
5:04
Just clear the screen here.
5:13
And that works like we expect
because we did supply the block.
5:16
Now if we go back and comment
the block out, let's see what happens.
5:19
We just get the message
saying no block given.
5:25
Great Job!
5:30
Try practicing calling blocks in different
ways on your own now using WorkSpaces.
5:31
You need to sign up for Treehouse in order to download course files.
Sign up