5. Try It Yourself



I do not see anything doubled, and yet the self grader says that this is correct. Just what is the block {num * 2} doing here? I don't understand the point of this lesson, and think either the lesson or my code is wrong. (The output is 7 everywhere.}

def double(num)
    puts "before yield " + num.to_s
    yield num
    puts " after yield " + num.to_s
    return num

num = 7
dbln = double(num) {num * 2}
puts "dbln is " + dbln.to_s


the num 2 doubles it atomaticaly in the computer.


what gets doubled and where? how is this useful? I don't see how get my hands on a doubled variable. If this is all internal, then how is it useful?


It appears that the scope of the inserted block is local and num is still 7 after the yield with
def double(num)
puts "before yield " + num.to_s
yield num
puts " after yield " + num.to_s
return num

num = 7
dbln = double(num) {puts "num doubled: " + (num * 2).to_s
num = num * 2}
puts "dbln is " + dbln.to_s`

This output is still:

before yield 7
num doubled: 14
after yield 7
dbln is 7


puts " after yield " + num.to_s gets doubled its useful because its a shortcut. and i dont know about the "internal"part


This is exactly my point! It is not doubled, Look at my output after the yield both in the function boule the num is still 7 and the return value is 7.


yup. you're right. that's the part i scratch my had though


Try this to see when it get's doubled:
def double(x)
puts x
yield x
puts x

num = 5
double(num) { puts num*2}


I'm not sure of usefulness based on this particular code. But, it shows that the yield variable is local to the call. And the method has it's own 'num'. Not sure how helpful this is over all - but it does show where the code is doubled. so, 1/2 an answer.


I was also having trouble with this exercise, so I checked a couple of threads. I found your code, and used it to troubleshoot mine (hope that's OK).
Tried this and all it did was spit out an error-- codecademy error saying "did you create a method called double?"
I can't figure out why, since it's right up top.
My current code, which was literally me trying to troubleshoot it using your code as an example.

def double(x)
    puts x.to_s
    puts x.to_s
    return x

x = 95
z = double(x) {x = x * 2}
puts z.to_s

Yeah, not the best variable names-- sorry about that.


It seems to me that the ".to_s" and the "return" are unnecessary here. Wouldn't this work just as well?

def double (n)
 puts n
 yield (n)
 puts n

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


I was confused with this thread before i saw your post. It's exactly how mine was written out. It is correct.(my puts aren't necessary.)

def double (n)
puts "double you're number!"
puts "way to go!"

double(6) { |n| puts "You're number is #{n * 2}." }


Need help please.

my_array = ["raindrops", :kettles, "whiskers", :mittens, :packages]

Add your code below!

symbol_filter = lambda {|x| x.is_a? Symbol}
symbols = []
symbols << my_array.select(&symbol_filter)

Result in this:
[[:kettles, :mittens, :packages]]

But error message:
Oops, try again. Your symbols array should include kettles, but doesn't.



my code working
def double(n)

double(10) { |n| puts "#{n}"}


def double(n)
yield n

double(4) { |n| puts "You're number is #{n} * 2" }

my code worked!!


Your code not multiple.

It is more correct :slight_smile:
def double(n )
yield n * 2

double(4) { |n| puts "You're number is #{n} " }


Even my code works

def double(n)
yield (n)

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


doing the math in the method, and passing the change in the block, and puts'ing that worked for me :slight_smile:

def double(i)
puts "in the method"
yield i * 2

puts "what is i"
i = gets.to_i
double(i) {|i| puts i}


just code it simply, i'll share mine, the point is it want you to doubled the number input in the yield block. this code show the result 8.

def double(number)
puts "I'm before yield block!"
yield number.to_i
puts "I'm after yield block!"

double("4") { |number| puts number*2}

hope it helps! :slight_smile:


This is the simplest solution I've found that fits what the instructions call for and doesn't puts or do any unnecessary conversions (.to_s, etc.) . Thanks!