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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:36:32+00:00 2026-05-16T12:36:32+00:00

I have a hash in Ruby that is storing the word frequency of a

  • 0

I have a hash in Ruby that is storing the word frequency of a string, with the word as the key and the frequency as the value.

words = a_string.split(/ /)
freqs = Hash.new(0)
words.each { |word| freqs[word] += 1 }
freqs = freqs.sort_by {|x,y| y }
freqs.reverse!
freqs.each do |word, freq|
    puts word+' '+freq.to_s
end

I’ve read that hash iterators return the hash in a random order, but this seems to work so far.

Now I need to freqs hash to only contain the 20 most frequent words. How can I do this? Thanks for reading.

  • 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-16T12:36:33+00:00Added an answer on May 16, 2026 at 12:36 pm

    As you’ve probably seen, the reason it stays sorted is because your call to Enumerable#sort_by changes your Hash into an Array. From the documentation for that method:

    The current implementation of sort_by generates an array of tuples containing the original collection element and the mapped value.

    Once you have your sorted array, you could just use Array#first to get the top 20:

    top20 = freqs.first(20)
    

    If you want, you could use to_hash on the result to return it back to a Hash, but it will no longer be sorted.

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

Sidebar

Related Questions

I have a Ruby hash that contains a user-generated string. I need to pass
I have a ruby hash that I'd like to render using RABL . The
I'm kinda new to Flex. I have trying to send Hash from Ruby on
In Ruby 1.9.x I have a hash that maintains its order hsh = {9=>2,
Is there a hash implementation around that doens't remember key values? I have to
I have a Ruby hash that looks like this: h = {side1=>[green, Green], side2=>[verde,
I have a ruby hash that looks like this { stuff_attributes => { 1
I have a ruby hash that contains car makes and models followed by the
Let's say I have a hash in Ruby like this: d = {1 =>
I have a Ruby hash: @tags = { project_status => { title => Project

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.