FAQ: Loops & Iterators - Try It Out!

This community-built FAQ covers the “Try It Out!” exercise from the lesson “Loops & Iterators”.

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

Learn Ruby

FAQs on the exercise Try It Out!

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 am not sure if this will work for posting a question but here we go.
With this iterator layout or format:
odds = [1,3,5,7,9]
odds.each {|x| print x * 2}

How do I make sure a comma is added after each result?
Thank you.

2 Likes

You could use string interpolation…

 print "#{x.to_s},"

but then it would put a comma on the last value.

Another approach would be to use the map iterator which returns an array. Then use the join method to output a string.

puts odds.map( |x| (x * 2).to_s ).join(', ')

Hi everyone.

If I use the do version of this, it works fine. When I try it this way, it does not. Can anyone point out why? Thanks.

Note the effect that end braces have: They denote the boundaries of a code block. There is no do or end in that syntax.

odds.each { |x| print x  }

Since x is a block parameter it is not the same as the x you have on line 5, which is undefined at that point and should rightly raise an exception (throw a fatal error).

Hi mtf. Thank you for your reply.

How should I tell it to *2 each item in the array?

Thanks

1 Like

Just for printing?

odds.each { |x| print x * 2 }

Note that print does not move the draw pencil so each value is on the same line. To print on separate lines use, puts which includes a newline afterward (meaning the pencil is shifted to the beginning of the next line).

odds.each do |x|
    print x * 2
end

Note there are no end braces. do implies the start of a block, and end represents the end of the block. The block scope is the same as that of the curly brace block. Witness that both forms have the parameter variable inside the block.

Hello mtf and many thanks for your reply.

I understand the difference between print and puts, but I never heard of the ‘draw pencil’ before. Some new ‘syntax’ for me :slight_smile:

I think I understand now. I wasn’t including x * 2 in the block of code to be executed. I thought we had to write it like this though: x *= 2?

When I write it the do way, I find it easier to understand what’s happening on each line in plain English, but I don’t find it as easy using { }

Regarding the two ways to write the same code, is it recommended to practice both, or if I prefer one over the other, can I just use that one?

Thanks,

1 Like

One would be best to practice using both, even when only one form is expected. Complete a lesson, get the check marks, but don’t leave the page. Go back over your code and write it in the other form (without removing the other code) and run it. The result on the screen should be the same. Let yourself get used to recognizing both forms.

When it comes to the unit on Blocks, Procs and Lambdas you will be glad to have this in your toolkit. The block syntax ({}) is very much in use with those objects.

1 Like