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
Just like regular methods, blocks can also take arguments.
Code Samples
A block with an argument:
[1, 2, 3].each do |number|
puts number
end
Passing an argument to a block:
def get_name
print "Enter your name: "
name = gets.chomp
yield name
end
get_name do |name|
puts "That's a cool name, #{name}!"
end
Block arguments do not have to have the same name as the argument passed in:
def get_name
print "Enter your name: "
name = gets.chomp
yield name
end
get_name do |your_name|
puts "That's a cool name, #{your_name}!"
end
Methods that take blocks can return values normally:
def get_name
print "Enter your name: "
name = gets.chomp
yield name
name
end
name = get_name do |your_name|
puts "That's a cool name, #{your_name}!"
end
Just like methods,
blocks can also take arguments.
0:00
When we write a block
that takes arguments,
0:05
we can think of the arguments
as being sent in to the block.
0:07
Arguments are also pretty easy to write.
0:12
They're defined by using pipes in the name
of the variable that you want to access
0:15
inside of the block.
0:20
Let's see how that works now,
using work spaces.
0:22
So we've seen block arguments before, and
0:26
let's do a quick refresher
with the array each method.
0:29
So let's say we had an array of numbers,
and we're just gonna use 1, 2, and 3 here,
0:35
and then we're recalling each.
0:39
And then we would pass this block and
we can say number and
0:42
then just print out the number.
0:47
And then we can see that
the number is printed out for
0:55
each of the elements in the array.
0:58
Now what's gonna happen is the block is
going to be passed to the each method, and
1:01
then the argument is going to be
in between these pipes right here.
1:06
The argument is going to
be a variable called number
1:12
which we then have access to inside and
only inside the block.
1:17
If we were gonna try an access
number outside of the block,
1:21
we get undefined local variable or
method number.
1:25
I'm gonna exit IRB here and
clear the screen and let's go ahead and
1:29
create a new file and see how this
works when we write our own methods.
1:34
So went ahead and created this
file called blockarguments.rb.
1:39
And let's go ahead and
1:44
just create a really quick
method that will take a block.
1:46
So it's going to be get_name and
we'll just say enter your name.
1:51
Sign this to a variable.
2:00
And then end and
that is our method definition.
2:04
Now here’s how we would do the block.
2:11
Here’s how we would write it.
2:13
We would say get name and
then pass in the block.
2:14
So how do we access this?
2:19
Well we can use the yield keyword.
2:21
We know that we can jump back
out of a method into the block
2:23
when we use the yield keyword.
2:27
So if we're gonna do that, let's go ahead
and just print out, that's a cool name.
2:30
And we know we wanna print the person's
name, but how do we get it?
2:38
Well we can jump out of the block and
also yield a variable.
2:42
So we could say yield name,
which is gonna jump out of the block,
2:49
with this as an argument, and then we
would have access to it inside the block.
2:53
Now we're almost there.
3:00
Remember blocks need arguments.
3:01
So we are yielding one argument.
3:03
We can put that as
an argument to the block.
3:08
So now, if we go down here, and
run this, block_arguments.rb.
3:12
My name is Jason.
3:18
That's a cool name, Jason.
3:21
Awesome!
Thank you very much program that I wrote.
3:23
Let me just change that to puts so
that we get a new line here.
3:27
Okay.
3:32
Now when we create a method that takes
a block like this with an argument,
3:34
we have to access the argument
inside of the block.
3:38
If we just commented this out,
we would get an error.
3:42
And you can actually do comment in
between lines, anything after the pound
3:47
sign is going to be ignored by
the ruby interpreter for that line.
3:51
So if we were run this again,
let's go ahead and watch it error out.
3:57
Undefined local variable or method name.
4:01
So we don't have name inside of here,
4:05
we could if we wanted to comment that out,
and then we wouldn't get that error.
4:10
Since we're not trying to access it,
we don't have it error out.
4:18
And we're not limited to the same
variable name that we're yielding.
4:21
So it just goes into
the block as an argument.
4:30
For example,
we could do that's a cool name and
4:35
then change it into whatever we want, so
in this case I'm changing it to your name.
4:38
And if I run it again,
you'll see that it goes
4:43
back into the block as an argument and
works perfectly just like we expect it to.
4:48
So there's one other neat
thing we can do here.
4:53
Let's say that we wanted to
assign this to a variable outside
4:55
of the method definition and the block.
4:59
So let's say we wanted to assign
this to a variable called my name.
5:03
We could say my name is equal
to the result of this block, and
5:10
this isn't going to work quite like we
expected to, it's going to be blank.
5:16
So we can see here, my name is empty.
5:26
Well, why is empty?
5:30
Well we're just yielding the name.
5:31
We could also do an implicit
return of the name
5:34
and that would be the return
value of this method.
5:40
So the return value of the method gets
assigned to the my_name variable.
5:46
Let's run this one more time.
5:51
And we can see that it works and
5:55
the string Jason is assigned
to the my name variable.
5:57
Great job!
6:02
Try practicing writing blocks and
6:03
passing arguments to them on
your own now using work spaces.
6:05
You need to sign up for Treehouse in order to download course files.
Sign up