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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:03:04+00:00 2026-06-13T00:03:04+00:00

I am really new at ruby . I have created a function to count

  • 0

I am really new at ruby. I have created a function to count occurrences of words in a string. However, I am getting NoMethodError for + all the time. I searched, tried different variations, but couldn’t solve the problem. Here is the code:

def count_words(str)
    str_down = str.downcase
    arr = str_down.scan(/([\w]+)/).flatten
    hash = Hash[]
    arr.each {|x| hash[x] += 1 }
    (hash.sort_by {|key, value| value}.reverse)
end

Here is the error:

NoMethodError: undefined method `+' for nil:NilClass
    from ./***.rb:14:in `count_words'
    from ./***.rb:14:in `each'
    from ./***.rb:14:in `count_words'
    from (irb):137
  • 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-13T00:03:06+00:00Added an answer on June 13, 2026 at 12:03 am

    Change

    hash = Hash[]
    arr.each {|x| hash[x] += 1 }
    

    To

    hash = {}
    arr.each {|x| hash[x] =0 unless hash[x]; hash[x] += 1 }
    

    OR

    hash = Hash.new(0)
    arr.each {|x| hash[x] += 1 }
    

    EXPLAINATION

    hash = {}
    hash[1] = "example1" #ASSIGNMENT gives hash = {1: "example1"}
    p hash[2] #This gives `nil` by default, as key is not present in hash
    

    To give default value to the key which is not present in hash we have to do the following:

       hash = Hash.new("new value")
       p hash #Gives {}
       p hash[4] #gives "new value"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am really new with PHP and all I have to do is to
first of all, i am quite new to Ruby, although i have a strong
I am really new to Ruby and could use some help with a program.
I am really new to the programming but I am studying it. I have
I am really confused about Ruby on Rails REST routing. Even though I have
OK, so I'm really new to Ruby on Rails. I'm using InstantRails, if that
I'm really new to Ruby on Rails 3 and I'm working on a simple
I am pretty new with SQL and i have to translate this ruby script
I'm really new to both ruby on rails and programming. I am trying to
im really new to flash, how do i go about creating a variable which

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.