5. try it yourself

myGrades = [2, 22, 25, 17, 30,]
def double (grades)
grades = myGrades.each {|n|puts n*2}

It passed my code but “grades = myGrades.each {|n|puts n*2}” doesn’t seems right.
The hint indicates by using “method_name(argument) { block }” but I don’t know why does it has to be so complicated since I still don’t understand the concept.

Please help. This yield thing is really driving me nuts .

They’re not asking for any array, so you can get rid of that.

They want you to use your own method (here double), pass it an argument, and a block, using the syntax they showed you. They also want the block to multiply the parameter by 2 (but they don’t ask for any output).

hum, still don’t get it. would you mind showing me the correct answer and explain to me in detail?
appreciate it!

No, I never give full answers, because I don’t think it really helps in the long run, but in the other hand I can give hints and explanations on concepts. Sometimes I directly correct mistakes on small parts of code but that’s it.

What are you struggling with in my comment and/or the instructions? I’ll try to explain it more in details or in a different way.


okay, ill tell you my understanding first.
myGrades = [2,22,25,17,30] (assigning a block to a variable name myGrades
def double(grades) (defining a method named double and give it a argument of grades
yield(grades) (To execute the block below the end? don’t really understand yield function
okay heres is what i don’t understand how to make this code complete. myGrades.each{|n| n * 2} does the same thing to double everything in myGrades

1 Like

As I told you above, you don’t need an array in this exercise.

You should get rid of it because it seems to confuse you, and it’s not required in the instructions to pass the exercise.

Now that your array is gone (since you don’t need it), you don’t need to call .each either.

Great. Now, the instructions say:

Define your own method, double,
that accepts a single parameter and yields to a block. Then call it
with a block that multiplies the number parameter by 2. You can double
any number you like!

So, you defined your method double, so far so good. Your method accepts a single parameter, ok. Your method yields, no problem.
But where is the part I put in bold? Where did you call your method? Since you didn’t call it, you obviously never gave it a block, even less an argument. That’s what’s expected of you, and that’s what the syntax provided in the hint describes.

1 Like

As for the yield part, yes, it’s an instruction that will pass the execution to the block received by the method. In our exercise we also want to yield with an argument so that the block can work with it.

Oh and I forgot to correct your statement:

That’s not a block, that’s an array.

An array is a set of values, a block is a set of instructions.

This is a block:

puts "yolo"
puts "yili"

This is an array:

["yolo", "yili"]

HI - Sometimes you just don’t really understand concept and you get stuck - thats NORMAL but you just have to get through it, revise it and it will make more sense, more practice works wonders for me. Here’s my example:

def double(num)
puts "In the method, lets yield"
puts "Back to the method"


I think you get the above code easily but you seem to be confused how to call with block. Here’s how you call with block:

double(2) {|num| num * 2}

double = method name
(2) is argument you pass so it can be multiplied - can be any number
The rest is the block code, to multiply the value in the block by 2.
Hope that makes sense and explains a lot. I always go back to previous lessons to learn more and be able to do it easily - doesn’t mean I always understand it.


thank you very much! that helps a lot, I understand the concept now but I get stuck when it gets to more complex lol.

I was overthinking it as well. You only need four lines:

def double(x)
double(3) {|n| n * 2}


@deeqoo example is very good!
Here’s my example:

Example: def method_name(parameter) = double (some)
puts "Some text method! yield."
yield(“Some name”)
puts "Back in method!"
Example: method_name(argument) { block } = double(0) { (n) nx5 }

I agree with theapostate - think that is as tight as you can get it. I added in an optional puts line to make sure I knew what was going on:

def double(num)
    puts "We are going to double the number!"

double(6) {|num| num * 2 }

I had some trouble here as well. I suppose what confuses me is all the seemingly redundant parameters and arguments. Why do we put {|num| num … } when we are creating the block. I get that that is the syntax, but can someone please explain to me why we are using num and why twice? I have a pseudo-understanding of what’s going on here, but I would like to have a much deeper understanding. Help! :kissing_heart:


Mine was as simple as-

def double(n)

double(2) { |n| n * 2 }

Sometimes it can trip you up trying to puts or print a return when all the instructions are asking for is just the function.