5. Try It Yourself! - I struggled with this one a lot


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-L3ZCI/0/5?curriculum_id=5059f8619189a5000201fbcb


I struggled with this exercise for a while. I'm posting this in hopes maybe others can learn from my mistakes. I'd also like some assistance. I'll show where I went wrong and maybe someone can say "you should look over x again" which would be helpful.

So here's my working code:

def double(n)
  puts "This is before the yield"
  yield(6)
  n = n * 2
  puts "This is the value after the yield: #{n}"
end

double(5) { |n|
    n = n * 2
    puts "This is the value from the yield: #{n}"
    }

And here is what I was doing before I figured out the code above.

def double(n)
  puts "This is before the yield"
  yield
  puts n * 2
end

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

So let's have a look at how this works and what I was doing wrong. Here's the instructions for the exercise: 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!

Step 1: define the method which accepts a single parameter. Done using "def double(n)"
Step 2: yield to a block. done with "yield"
Step 3: call the method and multiply the parameter by 2 in the block

Let's go over what I was doing wrong.
Step 1 is fine, I defined the method with a parameter.

Step 2 is to put in a yield which I did. BUT that yield has to have a parameter. It has to look like this: "yield(6)" like in my correct code above. Why? Because we're calling the method with "double(5)" and the 5 takes the place of the n parameter. If we use "yield" then it's just going to give back an error.

Ok so the yield now has a parameter. Now it'll execute whatever is in the "double(5)" block which happens to be "{ |n| puts n * 2 }" So what's that going to do? It takes n (which is now 6 because it was defined in the yield parameter) and multiplies it by 2 and puts the answer. This is actually what we want but I don't want it to just put the answer, I want it to tell you where in the method it is. Before the yield, after, etc. So another hangup of mine was the block itself. I was trying to write everything on one line like in my bad code. AND I wasn't defining the "n = n * 2" variable. Once I figured out that I had to define "n = n * 2", I was able to "puts "This is the value from the yield #{n}"" like in the good code.

So with the addition of the parameter to the yield and the addition of "n = n * 2" to the block I was able to puts a line before the yield, go to the yield, multiply n * 2 and puts the result of the yield block, then finally complete the method by processing the line after the yield.

The line after the yield. So we're in the method but we're not in the yield any more. Which means n has the value it was originally given. We called "double(5)" originally so n is 5. To finally calculate n * 2 after the yield and "puts "This is the value after the yield: #{n}"" then just do like I did in the yield's block; make "n = n * 2" and then put #{n} in your puts line.

I hope this helps someone else. If anyone has any suggestions as to what I should study due to my mistakes I'd be very happy to hear them. Also if you have corrections please post. Thanks.

I also found this video in another thread that might help with the yield command https://www.youtube.com/watch?v=ObR8UuaPc6Q


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.