# 2 Collect `Em All

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

doubled_fibs = fibs.collect {|n| n ** 2}

This code still seems to give me an error about not having 2 in it, can anyone help or explain . I read the other two topics, but can’t seem to figure it out .

SOLVED by thinking outside the box a little. Here’s the code I had to write to get a green button finally to light up when the previous code gave a continuous error message about not having 2 in it :

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

double = Proc.new {|x| x * 2}

doubled_fibs = fibs.collect(&double)

just used the proc from lesson 7

4 Likes

wow, thanks novik21, ha!

usei seu codigo, apenas trocando a potenciação pela multiplicação e funcionou.
doubled_fibs = fibs.collect {|n| n * 2}

10 Likes

you don’t need Proc at all. (i haven’t studied that yet)
here’s my code!

y = fibs.collect { |x| 2*x}

doubled_fibs = y

side note - This task is little tricky in the sense that it wont work if you put “puts” to print the result

For a one line fix, this works lovely…

`doubled_fibs = fibs.collect {|x| x * 2}`

7 Likes

This is what I used guys and it worked perfect.
fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

doubled_fibs = fibs.collect{|fibs| fibs * 2}

Actually if you put the “puts” before the y = fibs.collect{|x| x2} it should work iirc. Also as a side note 2x is generally considered really bad form in most programming languages. To be honest, it’s something I had learned long ago but I can’t remember “why” it was bad. I think it had to do with readability.

Thanks for correcting me with the right form. Yeah “puts” definitely does that job but iirc, that exercise had a bug. So, instead it would give me an error. Some exercises in here is buggy with Mozilla FF.

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

doubled_fibs = fibs.collect { |num| num * 2 }

doubled_fibs

I just did it like this

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

doubled_fibs = fibs.collect {|x| x*2}

It seems a lot of people (including me… thats why I am here) confused doubled_fibs (multiplied by 2) as if they were asking for squared_fibs (raised to the power of 2). When code academy instructions said “similar to the example above” didn’t mean “exactly as”.

1 Like

First tried:
doubled_fibs = fibs.collect {|n| n **2}

This gave an error so i tried a lot of different things, but here below is my working code.

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

doubled_fibs = fibs.collect {|n| n *1}
doubled_fibs.collect! { |n| n *2}

Here’s my code it’s giving me an error saying “Did you create a varible double_fibs?” Not sure what’s wrong here.

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

double_fibs = fibs.collect { |x| x * 2 }

try this code
fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
doubled_fibs=fibs.collect {|num| num*2}

doubled_fibs

Here is my code. Works for me!

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

doubled_fibs = fibs.collect { |x| x * 2}

This code worked perfectly fine for me!

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

doubled_fibs = fibs.collect { |num| num * 2}
puts doubled_fibs