Learn
You can also pass parameters to yield
! Check out the example in the editor.
- The
yield_name
method is defined with one parameter,name
. - On line 9, we call the
yield_name
method and supply the argument"Eric"
for thename
parameter. Sinceyield_name
has ayield
statement, we will also need to supply a block. - Inside the method, on line 2, we first
puts
an introductory statement. - Then we yield to the block and pass in
"Kim"
. - In the block,
n
is now equal to"Kim"
and weputs
out “My name is Kim.” - Back in the method, we
puts
out that we are in between the yields. - Then we yield to the block again. This time, we pass in
"Eric"
which we stored in thename
parameter. - In the block,
n
is now equal to"Eric"
and weputs
out “My name is Eric.” - Finally, we
puts
out a closing statement.
Instructions
1.
Call yield_name
with your name as a parameter. Make sure to pass in a block that puts
“My name is #{your name here}!”
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.