5. try it yourself


#1

myGrades = [2, 22, 25, 17, 30,]
def double (grades)
yield(grades)
end
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 .
Thanks


#2

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).


#3

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


#4

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.


#5

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
end
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


#6

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.


#7

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"]

#8

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"
yield(num)
puts "Back to the method"

end

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.


#9

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


#10

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

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


#11

@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!"
end
Example: method_name(argument) { block } = double(0) { (n) nx5 }


#12

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!"
    yield(num)
end

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

#13

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:


#15

Mine was as simple as-

def double(n)
yield(n)
end

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.