FAQ: Blocks, Procs, and Lambdas - Collect 'Em All

This community-built FAQ covers the “Collect 'Em All” exercise from the lesson “Blocks, Procs, and Lambdas”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Collect 'Em All

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello. I thought I understood this pretty well until I had to access the answer. Can someone please explain it to me? I looked up Proc and .new but I still don’t think I completely understand it.

Thanks in advance for any help.

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

Add your code below!

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

doubled_fibs = fibs.collect(&double)

puts doubled_fibs

The code I wrote was:

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

Add your code below!

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

puts doubled_fibs


2 Likes

Hello,
Could somebody explain the difference between .collect and .each?
What I understand is that both pick each element of an array and perform a given task.

3 Likes

collect returns an array. each returns only the input array, and looks to ignore the action in the block.

double_fibs = fibs.each {|x| x * 2}
puts double_fibs

Output

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

The output array would have to be created initially, then the each method could iterate over the input array, operate on each element then push it to the output array.

Now let’s create an output array

double_fibs = []
fibs.each {|x| double_fibs << x * 2}
puts double_fibs

Output

[2, 2, 4, 6, 10, 16, 26, 42, 68, 110]
6 Likes

I think this exercise is totally messed up. The examples doesnt help at all. I tried to copy in the above examples but it doesn’t work. Im not sure if its only me or something else but this is not the first time I don’t get a whole lesson because of the poor explanation. The “solution” gives me something totally new that I have never seen before… I hope others will succeed

17 Likes

There’s a couple ways to do this, but the way I found that best follows the lesson is this:

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

You’re right! This answer does make the most sense for what we have learned thus far, when compared to the solution they provide you.

4 Likes

Hi,

In the exemple:

my_nums = [1, 2, 3]
my_nums.collect { |num| num ** 2 }

==> [1, 4, 9]

How can we check this values?

You could take that list and see if it transforms back to the original using the inverse of square, namely square root.

my_nums = [1, 2, 3]
squared_nums = my_nums.collect {|num| num ** 2}

roots = squared_nums.collect {|num| num ** 0.5}
1 Like

got it!! that’s a cool strategy ^^. thanks!

note: squared_nums.collect right?

1 Like

Right. That was a typo, now fixed. Thanks.

1 Like

I thought the explanation was simple enough but when I kept getting errors I chose to view the solution, once I did I was completely confused because there were some things I never saw before. Proc and &double notably. I was left very confused.

3 Likes

&double is a Proc. Which exercise are you on?

me too i do not understand it

what is the proc doing in the code ?

It is the callback for the array.collect() method. That block of code will execute for every element in context array.

I don’t think your the only one. I think the biggest problem with coding is its very complicated and the teaching of it is often sub par or flat out awful.

1 Like

Hard to have a valuable opinion of the quality of teaching when the subject is completely unknown. Learning is the responsibility of the learner. Criticizing a teacher gets them nowhere. Only when committed to due diligence does a learner get anything of real value from a subject. It does not come from the teacher, they are only the presenters. Value comes from what we put into a subject, be that interest, effort, excitement, experimentation, exploration, etc.

you explain/convey information a hundred times more effective than the solution on this lesson, and past lessons. teaching CS is a skill, like teaching violin as I did, and you display information and answer questions in a way that while challenging, remains accessible to one who is determined, curious and willing to explore the subject. thank you.

1 Like

I think the issue many of us are running into, is that the accepted solution for the problem includes pieces of code that have yet been revealed to use, while the rest of the exercises use things we’re currently learning, or have been discussed. the solution is the first time we’ve seen “.proc”.
I go the correct output by using code topics we’ve already learned, but it was not accepted as an answer. We can look at the solution and then go look things up, but then that defeats the purpose of having the challenge, in my opinion.