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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:25:37+00:00 2026-05-14T07:25:37+00:00

Given this hash in ruby: h={ 2010-03-01=>2 2010-03-02=>4 2010=03-03=>1 .. .. n days with

  • 0

Given this hash in ruby:

h={
2010-03-01=>2
2010-03-02=>4
2010=03-03=>1
..
..
n days with working hours
}

where hash key is a date and hash value is an integer, how do I transform this hash into a new hash where keys are weeks/months/years (with aggregation)?

Let me show an example with weeks, the end result should look like:

h_weeks={7=>36, 8=>42, 9=>34 ..,..,n}

with months:

h_months={1=>170, 2=>180, 3=>146}

with years:

h_years={2009=>950, 2010=>446}

where key is a week number(month/year), and value is an aggregate of working hours within this week/month/year.

I’m writing a small working hours tracking application and would like to group this data.

Thanks.

  • 1 1 Answer
  • 1 View
  • 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-14T07:25:37+00:00Added an answer on May 14, 2026 at 7:25 am
    h = {
      Date.parse('2010-04-02') => 3,
      Date.parse('2010-05-03') => 5,
      Date.parse('2009-04-03') => 6
    }
    by_year = Hash.new(0)
    by_month = Hash.new(0)
    by_week = Hash.new(0)
    h.each{|d,v|
      by_year[d.year] += v
      by_month[d.month] += v
      by_week[d.cweek] += v
    }
    

    or, slightly more exotic:

    aggregates = {}
    h.each{|d,v|
      [:year, :month, :cweek].each{|period|
        aggregates[period] ||= Hash.new(0)
        aggregates[period][d.send(period)] += v
      }
    }
    p aggregates
    #=>{:year=>{2010=>8, 2009=>6}, :month=>{4=>9, 5=>5}, :cweek=>{13=>3, 18=>5, 14=>6}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this hash that comes though :params positions=>{ 1=>{x=>50, y=>30}, 2=>{x=>22, y=>53}, 3=>{x=>68, y=>35}
Given this markup: // Calendar.html?date=1/2/2003 <script> $(function() { $('.inlinedatepicker').datepicker(); }); </script> ... <div class=inlinedatepicker
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
Given this Itemgroup: <ItemGroup> <Foo Include=First> <Value>1</Value> </Foo> <Foo Include=Second> <Value>2</Value> </Foo> </ItemGroup> How
In extensions to ruby RARRAY_PTR() which returns a VALUE* for a given ruby array
Possible Duplicate: Reversing an MD5 Hash Given this method in c# public string CalculateFileHash(string
Given this XML: <?xml version=1.0 encoding=utf-8?> <content dataType=XML> <item type=Promotion name=Sample Promotion expires=04/01/2009> <![CDATA[
Given this variable in tcsh: set i = ~/foo/bar.c how can I get just
Given this object class Contact { public IEnumerable<Person> People { get; } public string
Given this class public partial class Default : Page { private IRepository repo; ...

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.