FAQ: The Zen of Ruby - The Right Tool for the Job

This community-built FAQ covers the “The Right Tool for the Job” exercise from the lesson “The Zen of Ruby”.

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

Learn Ruby

FAQs on the exercise The Right Tool for the Job

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!

here’s my solution but i got an eror message

Blockquote
my_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_array =
my_array.each { |x| if x % 2 === 0 push.even_array}

that’s the error message
(ruby):3: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ‘;’ or ‘\n’
my_array.each { |x| if x % 2 === 0 push.even_array}

The argument is missing…

even_array.push(x)

However, Ruby gives us an even simpler method: select

even_array = my_array.select { |x| x % 2 === 0 }
1 Like

What kind of situation calls for the use of yield ? I understand the exercise, but am having a hard time imagining an example use.

my_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
puts “#{my_array.select { |x| x % 2 === 0 }}”

This is my solution and I don’t get any errors and it puts out only the even numbers. Unfortunately I can’t go on to the next exercise, the button is still greyed out. :frowning:

@asimeir
For starters, I’d read the error message:
(ruby):3: syntax error, unexpected identifier, expecting keyword_then or ‘;’ or ‘\n’
It just states that Ruby thought you were going to put the keyword “then”, a semicolon, or add a new line.

@sebastian.remm984130
That prints a list. I believe that the lesson wants you to print each item on a new line.


(Also, there’s a subjectively simpler way to implement it - Ruby’s one-line if.)

1 Like

Hi folks,

I applied the list idea by byteblitz and it worked quite well. Unfortunately, my code has several lines and needs definitely improvement. I tried many ways to print each item on a new line with as less lines as possible. I have also no glue how to implement a Ruby´s one-line if.


my_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_array = my_array.select {|x| x % 2 === 0}
puts even_array[0]
puts even_array[1]
puts even_array[2]
puts even_array[3]
puts even_array[4]

#Variant 1: puts “#{my_array.select {|x| x % 2==0}}”
#<= Just listed the entire Array in one line

#Variant 2: puts my_array[n] if my_array.select {|n| n % 2 === 0}
#<= Said undefined local variable or method ´n´for #

Since I am not happy with my solution may I ask anyone to give me another hint how to improve?

Thanks a lot folks.

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

my_array.each do |x|

if x % 2 == 0

puts x

end

end

That one worked for me.

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

my_array.each do |num|

puts num if num % 2 == 0

end

or

my_array.each { |num| puts num if num % 2 == 0 }

2 Likes

Why does this not pass the exercise?

puts my_array.select {|x| x.even?}

this worked

my_array.each do |num| puts num if num % 2 ==0 end

puts my_array.find_all {|x| x.even?}

make this way, works too but without next step

yep) works, but there are a lot of ways to made this one

They fail to explain how the hint works. The API certainly does not explain it at all. Did they even check the link before posting it here?

the method in the hint was -

.even?

My solution:

my_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_array.each { |i| if i.even? then puts i end}

1 Like

Ruby lets us simplify this even further (pun not intended):

y_array.each { |x| puts x if x.even? }
1 Like

Very nice, thank you

1 Like

Hi!

A variation of what I’ve been seeing others have done, but not passing. I get the following message: It looks like you didn’t print anything to the console using puts!

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

my_array.each { | n | puts n if n/2 == Integer }

I know now there’s a easier way, but this is what I figured out before checking the forum.

I’ll appreciate any feedback!