9.A vous de jouer ! (Blocs, procs et lambdas)


#1

I type:
def bonjour
yield
end

phrase = Proc.new { puts "Bonjour !" }
bonjour.select(&phrase)

and Ruby said me:
Oups, merci de réessayer. Votre syntaxe est incorrecte. no block given (yield)

Please, someone could help me?
Thks


#2

The goal is to give the proc to bonjour, not to select. At least I think, I don't know which exercise it is, it should be in the instructions.


#3

Thks for your reply

Ok, instructions said
1- to create a def 'bonjour' without (n) and with a yield.
2- to create a proc 'phrase' which puts 'bonjour!' and then to link the proc to the def 'bonjour'
No more instruction. I don't understand why Ruby said 'no block given (yield)'

It's the exercise 9 of the block proc and lambda of Ruby.
Trick said:
1-def name_methode
yield
end
2-name_proc = Proc.new do
puts "Text"
end

So…???


#4

Because the exercise is expecting you to give the proc to the bonjour method, not to the select method.

That's what they mean by: "link the proc to the def 'bonjour'"


#5

Ok, sorry, so I don't understand what's the difference between 'give a proc to a method' and 'select a method' in terms of code. Could you show me examples please?


#6

You don't select a method. select is a method itself.

By calling bonjour.select(&phrase), you're calling the method bonjour and chaining it to the method select which will receive the proc phrase. But that's not what's expected. They expect you to give the proc directly to the bonjour method.

How do you think you will do that (just take inspiration from how select receives the proc)?


#7

lol, you make me crazy, it's 1:20am in France, I'm tired, and it's a little bit difficult for me to translate in English ! But I really appreciate that you want make me found by myself the solution… I try


#8

That's what you get for coding so late ^^. I'll give you the answer if you don't succeed.


#9

I'm going to sleep now, I'll give you another hint:

When you type:

puts "Bonjour!"

You're giving the object "Bonjour!" to the method puts.

Another way to type this is:

puts("Bonjour!")

#10

I read and read last 3 lessons, and I don't understand..maybe a problem of translation between french and english ?
My method is 'bonjour'
My proc is 'phrase'
My goal is to give the proc 'phrase' to the method 'bonjour'
vbv ggbvf (<-- sorry it's my cat)
so I give a proc like that (&phrase)
and we have bonjour(&phrase)
I try … drums rolls...


#11

GOOOOODDDDDDD !!! lol


#12

Many thanks for your patience


#13

Great that it worked. Good luck for the rest of the course.