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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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]}”
end
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”
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…
5
10
15
25
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.
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)
my_block.call(self)
end
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.