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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:07:39+00:00 2026-05-26T23:07:39+00:00

I am wondering what purpose does the counts variable serve, the one right before

  • 0

I am wondering what purpose does the counts variable serve, the one right before the last end?

# Pick axe page 51, chapter 4

# Count frequency method
def count_frequency(word_list)
    counts = Hash.new(0)
    for word in word_list
        counts[word] += 1
    end
    counts    #what does this variable actually do?
end

puts count_frequency(["sparky", "the", "cat", "sat", "on", "the", "mat"])
  • 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-26T23:07:39+00:00Added an answer on May 26, 2026 at 11:07 pm

    The last expression in any Ruby method is the return value for that method. If counts were not at the end of the method, the return value would be the result of the for loop; in this case, that’s the word_list array itself:

    irb(main):001:0> def count(words)
    irb(main):002:1>   counts = Hash.new(0)
    irb(main):003:1>   for word in words
    irb(main):004:2>     counts[word] += 1
    irb(main):005:2>   end
    irb(main):006:1> end
    #=> nil
    irb(main):007:0> count %w[ sparky the cat sat on the mat ]
    #=> ["sparky", "the", "cat", "sat", "on", "the", "mat"]
    

    Another way someone might write the same method in 1.9:

    def count_frequency(word_list)
      Hash.new(0).tap do |counts|
        word_list.each{ |word| counts[word]+=1 }
      end
    end
    

    Though some people consider using tap like this to be an abuse. 🙂

    And, for fun, here’s a slightly-slower-but-purely-functional version:

    def count_frequency(word_list)
      Hash[ word_list.group_by(&:to_s).map{ |word,array| [word,array.length] } ]
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering, for no other purpose than pure curiosity (because no one SHOULD EVER
I was wondering if it's possible to emulate a big-endian behavior, for testing purpose?
I see <leader> in many .vimrc files, and I am wondering what does it
I am wondering what is the purpose of encoding the String: "login:password" in base
So I just created an application that does page scraping for me, and ran
Wondering what a continue statement does in a do...while(false) loop, I mocked up a
This is strictly an opinion/experience question for research purposes. I was wondering what coding
Wondering if anyone has gotten the infamous database is locked error from Trac and
Wondering if there is any Text to Speech software available as a plug in
Wondering if I need to do something in my swf to be able to

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.