FAQ: Blocks, Procs, and Lambdas - Create Your Own!

This community-built FAQ covers the “Create Your Own!” 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 Create Your Own!

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!

Using the code:

puts greeter(&phrase)

the console additionally prints what seems to be a character count (beginning at 9). It reads 21 if you instruct the proc to print “Hello there!”.

Is using puts on a method problematic?

4 Likes

If I understand correctly “def greeter(n)” >>n is a parameter, not an argument?
I’m not sure if I’m supposed to be writing anything with yield (the explanation of yield in this chapter was severely lacking and I would’ve appreciated a bit more theory or something to explain exactly what it’s doing and if I’m supposed to write arguments/parameters after it…?
Regardless, I’ve written this below and then checked the hint and it was exactly what I’d already written, and it prints out what it’s supposed to, but I get an error saying this: “Did you call greeter(&proc) so that it puts ‘Hello there!’ to the console?”
Which I answer “yes” to, because it does print that as it’s meant to, but I know I’m getting this error because of some stupid syntax error, that it wants me to hit return between lines somewhere or something, or I that I was supposed to put something in parenthesis around the method and I’m getting confused by the wording of the questions here (yet again its not as explicit as I need it to be, this is getting tiring)

def greeter
yield
end

phrase = Proc.new {|x| puts “Hello There!”}

greeter(&phrase)

3 Likes

asked for solution - turns out i dont have to put |x| in the block every time I make one? I was not aware of that…why is this/how does it work?

2 Likes

Because of the explanation of what you have to do and the example on Hint, I don’t get why you should add --> greeter(&phrase) at the end. See the complete solution below:

def greeter
yield
end

phrase = Proc.new { puts “Hello there!” }

greeter(&phrase)

Bit late to the party but for those that will follow, that is simply because you’re printing twice to the console (within the &phrase, and the puts here just before greeter).

So to print the “Hello there”, you need to call the method greeter with a block which is the proc ‘phrase’

Did I miss something in an earlier lesson about defining methods without parameters, or was that not covered? My assumption would be syntax defining a method without a parameter would be invalid, because a method couldn’t take an argument without a parameter. Aside from completing this exercise, why would you ever define a method without a parameter?

def pin
    "1234"
end

In Ruby it is common to define variables using a method. These methods do not need a parameter since their only purpose is to return the value of that variable.

puts pin    # 1234

This doesn’t answer OP’s question. In your case nothing is being passed in, the method just returns a value, in this exercise a code block is being passed in and yielded, all without referencing the parameters in the method. I’m also confused about this.

yield allows additional instructions to be passed to a method. There is no parameter. We see in this example how the block argument is immediately referred to by the yield keyword. Without the block an error would result. yield is calling the block.

Hi guys,

I’ve got a question on this exercise.

How does the defined greeter method know to yield to the newly created phrase proc if it’s not called out / defined within the method itself?

def greeter
  yield
end

phrase = Proc.new { puts "Hello there!" }

greeter(&phrase)

Thanks!
Johan

It gets defined and assigned to phrase which is then passed as a Proc to the greeter method. yield acts upon the block.