4. Yielding with Parameters


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-L3ZCI/0/4?curriculum_id=5059f8619189a5000201fbcb#


The hint for this lesson reads:
"Your code should look identical to the code on line 7, only your name should be between yield_name's parentheses."

Unfortunately, line 7's only content is end. Intuitively, I'm inclined to assume the code on line 10 is what is being referenced but that didn't work for me either. I'm very new to Ruby as well as coding at large. I'd appreciate any help I can get!


def yield_name(name)
  puts "In the method! Let's yield."
  yield("Kim")
  puts "In between the yields!"
  yield(name)
  puts "Block complete! Back in the method."
end

# Now call the method with your name!
yeild_name("Dave") { |n| puts "My name is #{n}!" }


#2

Your code looks fine save for one error: yeild_name.

Not sure this is not just an SCT issue. I cannot get the correct code to pass.

def yield_name(name)
  puts "In the method! Let's yield."
  yield("Kim")
  puts "In between the yields!"
  yield(name)
  puts "Block complete! Back in the method."
end

# Now call the method with your name!
yield_name("Roy") { |n| puts "My name is #{n}." }

Output

In the method! Let's yield.
My name is Kim.
In between the yields!
My name is Roy.
Block complete! Back in the method.
nil

This is what I got to pass:

def yield_name(name)
  puts "In the method! Let's yield."
  yield("Kim")
  puts "In between the yields!"
  yield name
  puts "Block complete! Back in the method."
end

yield_name("Eric") { |name| puts "My name is #{name}." }

# Now call the method with your name!
yield_name("Roy") { |name| puts "My name is #{name}." }

Output

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 Roy.
Block complete! Back in the method.
nil

However, it runs counter to what is asked for. Not sure what the issue is or if will ever get tracked down and fixed.


#3

Thank you! Like the quick response! :slight_smile:


#4

https://www.codecademy.com/courses/ruby-beginner-en-L3ZCI/0/4?curriculum_id=5059f8619189a5000201fbcb#.Kindly rectify the problem


#5

Is that an order? Not much any of us can do with this issue but work around it.


#6

I don't understand Yields, please someone explain.
What I understood, please correct me if I am wrong:
1. for the first yield("Kim") in the method, logic will branch to the first block outside of the method and print "my name is Kim.
2. for the second yield(name) in the method, logic will branch to the first block again and use parameter Eric, prints 'my name is Eric" then we have "block complete"
3. then the second block is executed calling yield_name method with my name, repeating Kim and when it comes to the second yield in the method it will branch to the second block?
what is the rule here, trying to understand the rule


#7

In a method of some name, a yield will look at the block that is passed to the method, and insert it into the process thread, as we see in this lesson.

If we look closely, we see that the block is not a named object, but a call to the method, with the block as a parameter.


#8

I got it, thanks. We call a method with a block and yield to that block with the different parameters.