FAQ: Methods, Blocks, & Sorting - Using Code Blocks

This community-built FAQ covers the “Using Code Blocks” exercise from the lesson “Methods, Blocks, & Sorting”.

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

Learn Ruby

FAQs on the exercise Using Code Blocks

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!


Currently, the second .each will print out 5 five times (since it will print 5 for each item in the array, and there are five items). Modify the block so it will print each item in the array multiplied by five.

method that capitalizes a word

def capitalize(string)
puts “#{string[0].upcase}#{string[1…-1]}”

capitalize(“ryan”) # prints “Ryan”
capitalize(“jane”) # prints “Jane”

block that capitalizes each string in the array

[“ryan”, “jane”].each {|string| puts “#{string[0].upcase}#{string[1…-1]}”} # prints “Ryan”, then “Jane”

Im stuck

itś somekind of bug or an error, just refresh the webpage and you will have the rigth exercise.

[1, 2, 3, 4, 5].each { |i| puts 5 }

this one can be solved multiplying every item by 5 like this

[1, 2, 3, 4, 5].each { |i| puts i*5 }

I tried:

[1, 2, 3, 4, 5].each { |i| puts i * 5 }

[1, 2, 3, 4, 5].each { |i| }

as a result, I received:

system allowed me to proceed to next exercise.

not sure if I am correct, but the solution it gave did not make sense to me or give the answer : 5, 10, 15, 25

Put in the same solution and got the correct results. Late reply lol but yeah your answer is correct it seems.

I don’t know if you’ll get this, or even, if you are still stuck on it?
But from what I understand:

[1, 2, 3, 4, 5].each { |i|
Means each array in [ ] becomes a ‘placeholder name’ as ‘i’.

.each { |i| puts i * 5 }
Means for each ‘i’, times it by 5.
so it now becomes:
1 x 5
2x 5
3x 5
4x 5
5x 5
which in order, the answer to those calculation will be…

I hope that makes sense :slight_smile:

The lesson states that the .each method has been used without the optional parentheses () thus far in the examples, I wonder what the actual syntax is for using this method with the parentheses, as I had no luck proving that by wrapping the parentheses around the code block that normally follows the .each method. The output throws an error when I try to do that.

1 Like

yeah Im on there right now and I really want to know what they were hiding

I was confused by this part as well. As you said, wrapping the whole block inside parentheses triggers an error, but this works, so maybe that’s what they meant?

[1, 2, 3, 4, 5].each() { |i| puts i }

From what I read, it is possible to define a method so that it accepts blocks by writing for example:

def my_method(&my_block)

And then, if my understanding is correct, it would allow you to use this method like this:

my_method { puts “Hello” }

(To give a completely fictional example)

So maybe that’s what they meant? But since this is different from what we have seen so far, I find the first paragraph of this lesson quite confusing… The " You just didn’t notice because we didn’t use the optional parentheses. We are sneaky." makes it sound like it should be obvious to us what is going on when we realise there are parentheses, but I was still confused after realising “[1, 2, 3, 4, 5].each() { |i| puts i }” worked because I didn’t see how the block was still considered a parameter/argument even though it was outside the parentheses… If I’m wrong and this paragraph actually means something else, I would be happy to hear the explanation.