Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 865221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:36:26+00:00 2026-05-15T09:36:26+00:00

UPDATE 2 : For posterity, this is how I’ve settled on doing it (thanks

  • 0

UPDATE 2: For posterity, this is how I’ve settled on doing it (thanks to Jorg’s input):

100.step(2, -2) do |x|
    # my code
end

(Obviously there are plenty of ways to do this; but it sounds like this is the most “Ruby” way to do it; and that’s exactly what I was after.)


UPDATE: OK, so what I was looking for was step:

(2..100).step(2) do |x|
    # my code
end

But it turns out that I wasn’t 100% forthcoming in my original question. I actually want to iterate over this range backwards. To my surprise, a negative step isn’t legal.

(100..2).step(-2) do |x|
    # ArgumentError: step can't be negative
end

So: how do I do this backwards?


I am completely new to Ruby, so be gentle.

Say I want to iterate over the range of even numbers from 2 to 100; how would I do that?

Obviously I could do:

(2..100).each do |x|
    if x % 2 == 0
        # my code
    end
end

But, obviously (again), that would be pretty stupid.

I know I could do something like:

i = 2
while i <= 100
    # my code
    i += 2
end

I believe I could also write my own custom class that provides its own each method (?). I am almost sure that would be overkill, though.

I’m interested in two things:

  1. Is it possible to do this with some variation of the standard Range syntax (i.e., (x..y).each)?
  2. Either way, what would be the most idiomatic “Ruby way” of accomplishing this (using a Range or otherwise)? Like I said, I’m new to the language; so any guidance you can offer on how to do things in a more typical Ruby style would be much appreciated.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:36:27+00:00Added an answer on May 15, 2026 at 9:36 am

    You can’t declare a Range with a “step”. Ranges don’t have steps, they simply have a beginning and an end.

    You can certainly iterate over a Range in steps, for example like this:

    (2..100).step(2).reverse_each(&method(:p))
    

    But if all you want is to iterate, then what do you need the Range for in the first place? Why not just iterate?

    100.step(2, -2, &method(:p))
    

    This has the added benefit that unlike reverse_each it does not need to generate an intermediate array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.