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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:31:40+00:00 2026-06-05T01:31:40+00:00

I know how to achieve this, but it doesn’t seem like the ruby way

  • 0

I know how to achieve this, but it doesn’t seem like the ruby way of doing it.

I have a hash of days in week:

DAYS = {
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
6 => "Saturday",
7 => "Sunday"}

(there is probably some rails helper for this, but I’ve implemented it the fastest way I could think of)

Now I would like to create an array where the first element is today, and the other days should remain ordered.

Here’s this array

 def day_keys
    # proper sorting of days, start with today and then continue
    day_array = (1..7).to_a
    loop do
      break if day_array.first == Time.now.wday
      day_array.rotate!
    end
    day_array
  end

It’s Friday today, so it returns [5,6,7,1,2,3,4]. After this I map the values from this array with the keys from the DAYS hash to find the day names.

Solution seems pretty rough to me, so I would like to hear some ideas on how to implement this. I’ve spent some time looking at Array#cycle and Enumerable#inject but nothing seems to fit my needs.

  • 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-05T01:31:42+00:00Added an answer on June 5, 2026 at 1:31 am

    How about: ((Time.now.wday)..(Time.now.wday+6)).collect{|i| ((i-1) % 7) +1}

    If you are in rails, you have a few more helpers. This yields an array of Dates:

     ((Time.now.to_date)..(6.days.from_now.to_date)).collect{|i| i}
    

    And if you want the day name, it’s just a slight modification

     ((Time.now.to_date)..(6.days.from_now.to_date)).collect{|i| i.strftime "%A"}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do you know any elegant way to achieve this in ruby plz ? string
I have this code and don't know if what I would like to achieve
I want a Layout like this: But my code doesn't work. I can't achieve
I know how I would achieve this using PHP, but with jQuery / JavaScript
I know I could code what I'm trying to achieve, but I'd like to
im trying to achieve something but i dont really know how I have set
i'm bit stumped to know how to achieve favourite option i.e. i have a
As far as i know it's impossible to achieve the following, but only an
I have a method like this: public FbUser FindUserByGraphOrInsert(dynamic json, bool commit = false)
I have this database which contains a varchar. I want to know which records

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.