2/Collect them All


Can someone explain how they knew what code to write with these instructions?

These were the instructions,

We’ve created an array,fibs, and placed the first ten Fibonacci numbers in it.

Create a new variable, doubled_fibs, and set it equal to the result of callingfibs.collect. The block you pass to .collect should double each Fibonacci number, similar to the example above.
puts doubled_fibs if you want to see the final contents of the array.

I assumed this was the correct code to write,

doubled_fibs = fibs.collect
puts doubled_fibs

but the code the lesson gave me was,

double = Proc.new {|x| x * 2}
doubled_fibs = fibs.collect(&double)
puts doubled_fibs

I don’t understand how you were suppose to know to create a variable called double and then assign it to the value of Proc.new {|x| x * 2}. Also to add (&double) to fibs.collect The instructions said to create a new variable and created it equal to a result of calling fibs.collect.

What did I not understand?


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