5. Try It Yourself


#1




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


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
end

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


#2

the num 2 doubles it atomaticaly in the computer.


#3

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?


#4

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
end

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
nil


#5

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


#6

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.


#7

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


#8

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

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

output:
5
10
5

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.


#9

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
    yield
    puts x.to_s
    return x
end

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

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


#10

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
end

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

#11

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!"
yield(n)
puts "way to go!"
end

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

#12

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.

WHY?


#14

my code working
def double(n)
yield(n)
end

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


#15

def double(n)
yield n
end

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

my code worked!!


#16

Your code not multiple.

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

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


#17

Even my code works

def double(n)
yield (n)
end

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


#18

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
end

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


#19

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

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

hope it helps! :slight_smile:


#20

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!