[9/21] Procs: Savable blocks! No.9 Create Your Own


#1

My code below which follows the hint does work, but I don't really understand how the "flow" works...
May anybody explain what the instructions want us to make and why?

def greeter
yield
end

phrase = Proc.new do
puts "Hello there!"
end

greeter(&phrase)


#2

We want to define a method that will use a proc and then we want to use this method to see if it works.

First we define our method greeter with only a yield instruction.

Then we define our proc phrase with a simple puts in it.

Then we call our greeter method and pass it the proc.

Inside the method, it only has yield, which means that it will simply switch to the proc directly, then we're inside the proc, we use puts, and then the proc is finished.

Since a proc doesn't return to the method, we have nothing left to execute, the program ends (the method had nothing else to execute other than the yield anyway, so it would have ended anyway).


#3

So why is OPs code wrong? I wrote the exact same thing and can't find the error.


#4

His code is not wrong, he said himself that it works (and it should). If you have the exact same code and can't pass the lesson, try refreshing the page or deleting your browser's cache.


#5

Thanks for your help. I figured it out though. Little mistake. I had capitalized the "T" in "Hello there!". Just gotta pay more attention to detail I suppose.


#6

Wow - same issue... Glad I found this. Need this lesson to .downcase the response to check it! That was frustrating.


#7

I passed the lesson, but wanted for fun to take this one extra step. Is there a way we can pass &phrase and also use a unique name at the same time so it would say for example, Hello there John Doe!


#8

Me too. I don't understand what are they asking me to do.


#9

Here is the code to accomplish what you asked for(note I used puts statement to clarify the steps for you so you understand how the yield works)

def greeter
puts "We're in the method"
yield("John Doe")
end
phrase = Proc.new { |x| puts"Hello there #{x}!"}
greeter(&phrase)

and if you wanted to do what the lesson asked for just use this:
def greeter
yield
end
phrase = Proc.new {|x| puts"Hello there!"}
greeter(&phrase)


#10

Thanks for posting, this got me too. Annoying how little stylistic things make the program act like something is actually wrong. I get capitalization matters, but is it necessary within a string? Maybe i'm missing something


#11

Please why did you add |x| ?

Without that, it actually should work fine.

def greeter
yield
end
phrase = Proc.new { puts "Hello there!" }
greeter(&phrase)


#12

For me, it was a comma in the string that I had just randomly tucked in there. Thanks for the catch!


#13

This is the code that works for me:
def greeter
yield
end
phrase = Proc.new do
puts "Hello there!"
end
greeter(&phrase)


#14

A post was split to a new topic: Why do we pass &phrase? (9/21)


#15