FAQ: Blocks, Procs, and Lambdas - Yielding With Parameters

This community-built FAQ covers the “Yielding With Parameters” exercise from the lesson “Blocks, Procs, and Lambdas”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Yielding With Parameters

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

it says,

  1. On line 9, we call the yield_name method and supply the argument "Eric" for the name parameter. Since yield_name has a yield statement, we will also need to supply a block.

I don’t understand why “we will aslo need to supply a block”. thanks

Then we yield to the block and pass in "Kim" .

Notice that the block in question is the one bound to the call with ‘Eric’ as the parameter? That’s why we need a block. We’re defering execution of the inline code until that block is finished running, with a different value for n. It’s the same block that will run to print Eric’s name when that time comes.

yield is unlike return in that it doesn’t lose it execution context, only segues from it to another context, and back again. It keeps its bearings and the method carries on as if nothing happened. Well, that’s my less than well informed take of it. A wiser, better informed member may pipe in to correct me, which we will both welcome if I’m wrong.

1 Like

Why do we need to use yield, then add a block when we call the function? Why can’t we just continue to use put statements? It seems simpler that way, if all we are doing is printing things on separate lines.

What is return? What is execution context?

Think in terms of scope. Scope is the environment, or namespace in which a code segment operates. Functions and variables are all exposed to each other if the scope is common.

Now think of a caller, a statement that makes a call to a function.

func (args)   =>  caller in one scope  ...
=>  def func (params)  ... function gives another scope
=>  execution context (scope)
=>  return (optional values) ...
=>  back to caller scope
1 Like

Thanks for the reply. So execution context is the same as scope, which is the same as the environment, or namespace in which a code segment operates? Is a code segment the same as a function, or a block? I’m still not clear on why we would need to use yield instead of just using put statements for everything, in this exercise. Also, what is return and how does it lose its execution context?

Yes, and/or no. It could be a function, but it could also be inline code in global scope since Ruby allows that.

yield and puts are far removed from one another in terms of usage and effect. puts has no effect. yield reroutes execution flow without losing its place in its current block.

We need a more knowledgeable programmer to weigh in here as my explanations often skirt the edges and miss the meat of the matter.

return follows a direct path out of the function (which ceases at that point) back to the caller. It is also a vector along which can travel a data object which can be used in caller scope.

________              ________    caller scope
        \            /
       args       return
          \        /
           ________               function scope
1 Like

Thanks for the help. I don’t completely understand your explanations but I appreciate your feedback!

5 Likes

Hi everyone!

Why is “Eric” and “Adam” only printed once each? Is it because those lines of code are outside the yield_name method?

Thanks.

It is because the block is only run once for each of those names. For your name to print twice means the method was called twice. One for each of the other names.

1 Like

Thanks for your reply mtf.

Am I understanding this correctly:

Each block in the code only runs once, but the method runs twice? Why twice? Is ti because there are two blocks to run?

If there were three blocks, would the method run three times?

Thanks.

The method runs twice because it is called twice. If it were called a third time, yes, it would run again.

yield_name("Eric") { |n| puts "My name is #{n}." }
yield_name("Adam") { |n| puts "My name is #{n}." }
yield_name("Someone else") { |n| puts "My name is #{n}." }

=begin =>
In the method! Let's yield.
My name is Kim.
In between the yields!
My name is Eric.
Block complete! Back in the method.
In the method! Let's yield.
My name is Kim.
In between the yields!
My name is Adam.
Block complete! Back in the method.
In the method! Let's yield.
My name is Kim.
In between the yields!
My name is Someone else.
Block complete! Back in the method.
=end