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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:05:34+00:00 2026-05-29T11:05:34+00:00

I am a Ruby Rails newbie. Is there a way to know the popularity

  • 0

I am a Ruby Rails newbie.

Is there a way to know the popularity of elements in an Array over time?

For example lets say for the last 15 min..

The array has like [“abc”, “ab”, “abc”, “a”, “abc”, “ab”……..] being pushed into the array.. can we get “abc” and “ab” as the most popular ones.. just for the last 15 minutes?

If you take for an entire hour.. typical for the entire hour..”abcd” is the most popular.. it should return “abcd” as the most popular element in an array..

Is there a way to achieve this?

  • 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-29T11:05:35+00:00Added an answer on May 29, 2026 at 11:05 am

    Create your own class which inherits from Array, or delegates all its functionality to an Array. For example:

    class TimestampedArray
      def initialize
        @items = []
      end
    
      def <<(obj)
        @items << [Time.now,obj]
      end
    
      # get all the items which were added in the last "seconds" seconds
      # assumes that items are kept in order of add time
      def all_from_last(seconds)
        go_back_to = Time.now - seconds
        result     = []
        @items.reverse_each do |(time,item)|
          break if time < go_back_to
          result.unshift(item)
        end
        result
      end
    end
    

    If you have an old version of Ruby, which doesn’t have reverse_each:

    def all_from_last(seconds)
      go_back_to = Time.now - seconds
      result     = []
      (@items.length-1).downto(0) do |i|
        time,item = @items[i]
        break if time < go_back_to
        result.unshift(item)
      end
      result
    end
    

    Then you need something to find the “most popular” item. I often use this utility function:

    module Enumerable
      def to_histogram
        result = Hash.new(0)
        each { |x| result[x] += 1 }
        result
      end
    end
    

    On which you could base:

    module Enumerable
      def most_popular
        h = self.to_histogram
        max_by { |x| h[x] }
      end
    end
    

    So then you get:

    timestamped_array.all_from_last(3600).most_popular # "most popular" in last 1 hour
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a Ruby on Rails newbie. I'm learning Rails in my spare time (my
What is the best way to generate a random DateTime in Ruby/Rails? Trying to
I am a ruby and rails newbie. And I am working on a rails
I am a newbie when it comes to Ruby On Rails (and web programming
I'm a newbie to ruby, I want to know if I can use just
I'm a newbie to Ruby on Rails, and I'm wondering is it good to
Another newbie Ruby on Rails question: In my post view, I want to show
I'm a newbie ruby on rails programmer, so forgive me if this is a
hey I am newbie to ruby on rails and I am trying run a
I am a newbie to the ruby on rails platform and I was just

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.