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 8080863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:32:44+00:00 2026-06-05T16:32:44+00:00

Is there a more Ruby-ish and cleaner way to get a number to operate

  • 0

Is there a more Ruby-ish and cleaner way to get a number to operate within a range. If it hits the upper bound, the next number should be 1 again. Often seen in algorythms that e.g. force month 16 and 28 to be April (16-12=4).

The case: I have twelve images and N questions. Each question gets the next image, if all images are “used”, it should re-start at image 1.

def image_rotation(number) {
   uplim = 12

   return number if number <= uplim
   return image_rotation(number - uplim) if number > uplim
}

questions.each_with_index do |q,i|
  img = File::join("images", "image-#{image_rotation(i+1)}.jpg")
end

Note: I am not familiar with the terminology of this (kind of) problem, so searching stackoverflow did not give me any results or hints; but I cannot believe this question is not a duplicate, yet. Please point me to a keyword, answer/duplicate or feel free to answer it 🙂

And Bonuspoints for a one-liner. I need this in an ERB-file, so if I can keep the code clean, clear and simple, then I prefer to have it solved there, entirely.

  • 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-06-05T16:32:46+00:00Added an answer on June 5, 2026 at 4:32 pm

    Use modulo (%):

    def image_rotation(number) do
       upper_limit = 12
       number % upper_limit
    end
    
    image_rotation(12) #=> 12
    image_rotation(13) #=> 1
    

    If that’s all your method does, though, you could just do:

    i % 12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's probably a more efficient and more Ruby-ish way to do this: # Pad
Is there a more idiomatic/readable way of writing a get-and-set function in Clojure than:
is there a more simple way to get combobox (WinForms) selected items text? string
Is there a more elegant way of implementing the following logic in Ruby? a
Is there more elegant (less code) way of find a matrix OUT, with colSums(OUT)<=a
There are more than one solution for finding the-number-of-digits in a given number. For
Is there a more sensible way of doing this? I want to make a
is there a more effecient way to write this to only take up one
Is there are more efficient way than the following for selecting the third parent?
Is there a good way to chain methods conditionally in Ruby? What I want

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.