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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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
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.
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.
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
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.
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.
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