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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:22:57+00:00 2026-05-30T21:22:57+00:00

I new to Ruby and struggling to understand what is going on in this

  • 0

I new to Ruby and struggling to understand what is going on in this bit of code I wrote. Why do I have to declare two variables |x,y| to get the output I am expecting? I am only using x and y always appears to be nil. But when I change to |x|, my word count is always 0 (see code and output below). Thanks for any insight you may be able to provide.

def count_words(string)
  string.downcase!

  wordhash = Hash.new

  # what is going on here?  
  # Why do I have to have two 
  # variables in the scan block?
  string.scan(/(\b\w+\b)/){|x,y|
    wordhash.store(x,string.scan(/\b#{x}\b/).length)}

  return wordhash
end

puts count_words("Hello there.  This is bob bob bob")

# Correct Output with |x,y|:
# {"hello"=>1, "there"=>1, "this"=>1, "is"=>1, "bob"=>3}

# Incorrect Output with |x|:
# {["hello"]=>0, ["there"]=>0, ["this"]=>0, ["is"]=>0, ["bob"]=>0}
  • 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-30T21:22:59+00:00Added an answer on May 30, 2026 at 9:22 pm

    The other answer correctly explains why this doesn’t work as expected. Let me try to point out some more problems with your code:

    • string.downcase! modifies the argument given to the function, which is extremely bad style
    • /(\b\w+\b)/ you don’t need an additional match group here, simply use /\b\w+\b/. This will allow you to just use scan(...) do |x|, where x will be the matched word
    • wordhash.store(x,y) can simply be written as wordhash[x] = y
    • string.scan(/\b#{x}\b/).length you scan the string a second time, although that is not necessary. Instead, you can just increment a counter for every match of a given word.

    Example:

    def count_words(string)
      # set up a hash that accumulates the number of occurrences per word
      wordcount = Hash.new(0)
      string.downcase.scan(/\b\w+\b/) { |word| wordcount[word] += 1 }
      # no need to use return here, the function already evaluates to the last
      # value
      wordcount
    end
    
    p count_words("Hello there.  This is bob bob bob")
    # => {"hello"=>1, "there"=>1, "this"=>1, "is"=>1, "bob"=>3}
    

    This is just to demonstrate how your approach could be made working, in Ruby you’d probably solve this in a more functional way, preferrably using group_by, as Michael already demonstrated or using inject:

    string.downcase.split.inject(Hash.new(0)) { |h,word| h[word] += 1; h }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really new to Ruby (first day!) and I'm struggling with this here. I'm
I am pretty new with SQL and i have to translate this ruby script
I'm a new ruby/rails user trying to get my mac set up for the
I am a new Ruby on Rails user and had a question. I have
Not sure why I'm getting this, but I just installed RVM, the new Ruby
I'm new to Ruby, how do I do something like this? in C#, I
I'm a new member. I have been struggling with sqlitejdbc, I thought. I made
I am a Ruby newbie and I have been struggling with the following routing
I'm very new to Ruby, and currently running through the Ruby Koans to get
I am trying to get familiar with the new ruby selenium-webdriver as it appears

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.