Log in from a computer to take this course

You'll need to log in from a computer to start Learn Ruby. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store
Learn

Sooner or later, you’re going to need to perform a repetitive task in your programs. Many programming languages allow you to do this with a for loop, and while Ruby does include for loops, there are better tools available to us.

If we want to do something a specific number of times, we can use the .times method, like so:

5.times { puts "Odelay!" } # Prints 5 "Odelay!"s on separate lines

If we want to repeat an action for every element in a collection, we can use .each:

[1, 2, 3].each { |x| puts x * 10 } # Prints 10, 20, 30 on separate lines

Instructions

1.

Let’s get a little inventive. Write a loop that only puts the even values of my_array. (Bonus points if you use a one-line if!)

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?