FAQ: Blocks, Procs, and Lambdas - Symbols, Meet Procs

This community-built FAQ covers the “Symbols, Meet Procs” 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 Symbols, Meet Procs

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!

I’m curious about why we need to add the colon : to do the conversion successfully.

strings_array = numbers_array.collect(&:to_s)

My reading of the code above is: Ruby first has to turn to_s into a symbol and then turns it into a proc, so that the .collect method can use it legally

Is my reading correct? If it is, why do we need to turn it into a symbol first? Why can’t we just write the piece as below?

strings_array = numbers_array.collect(&to_s)

1 Like

I also wished there had been more explanation on this exercise

2 Likes

Why does the colon need to be there? Why are we turning to_s into a symbol?

2 Likes

Does anyone know why the following code doesn’t work?

numbers_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

numbers_array.each { |n| puts n.to_s }

It returns a list of integers anyway

Does it mean that .to_i, .to_str and other converting commands work only with .collect and .map?

The codeacademy lesson is pretty thin on the explanation. I found this to be a very good explanation: https://www.brianstorti.com/understanding-ruby-idiom-map-with-symbol/

5 Likes

https://www.codecademy.com/courses/learn-ruby/lessons/blocks-procs-and-lambdas/exercises/symbols-meet-procs

you can also convert symbols to procs using that handy little &.

Earlier, we were told that procs are actually converted to blocks using ‘&’, as the methods require a block rather than a proc. So, I am wondering if preceding a symbol with an ‘&’ does not rather convert them to a block rather than a proc.

The way that I understood it is this:

Symbols are used 1. as hash keys and 2. to reference method names.

Since to_s is a method, I read it as using :to_s to reference that method, and then & to turn it into a block so that .collect could use it.

1 Like

That code doesn’t work because it doesn’t create the array strings_array from the array numbers_array, plus it doesn’t make use of the ‘Symbol#to_proc’ syntax which is the point of the exercise.

What #each does is that it calls the given block once for each element in self, passing that element as a parameter. It returns the array itself (thus: numbers_array)

#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

whereas the instructions tell us to create a strings_array that should be the string version of the corresponding element from the numbers_array, returning:

#=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
1 Like