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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:48:05+00:00 2026-05-26T06:48:05+00:00

Despite there being many questions on nested hashes I’ve not found any solutions to

  • 0

Despite there being many questions on nested hashes I’ve not found any solutions to my problem.

I’m pulling in strings and matching each character to a hash like so:

numberOfChars = {}
string.each_char do |c|
    RULES.each do |key, value|
        if c === key
            numberOfChars[value] += 1
        end
    end
end

This worked fine and would output something like “a appears 3 times” until I realised that my hash needed to be nested, akin to this:

RULES = {
    :limb {
        :colour {
            'a' => 'foo',
            'b' => 'bar'
        },         
        'c' => 'baz'
    }
}

So how would I go about getting the ‘leaf’ key and it’s value?

While it’s iterating over the hash it also needs to count how many times each key appears, e.g. does ‘a’ appear more than ‘b’? If so add a to new hash. But I’m pretty lost as to how that would work in practice without knowing how it’ll be iterating over a nested hash to begin with.

It just seems to me an overly convoluted way of doing this but if anyone’s got any pointers they’d be hugely appreciated!

Also, if it’s not painfully clear already I’m new to Ruby so I’m probably making some fundamental mistakes.

  • 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-26T06:48:06+00:00Added an answer on May 26, 2026 at 6:48 am

    Are you looking for something like this?

    RULES = {
      :limb => {
        :colour => {
          'a' => 'foo',
          'b' => 'bar'
        },
        'c' => 'baz',
        :color => {
          'b' => 'baz'
        }
      }
    }
    
    def count_letters(hash, results = {})
      hash.each do |key, value|
        if value.kind_of?(Hash)
          count_letters(value, results)
        else
          results[key] = (results[key] || 0) + 1
        end
      end
      results
    end
    
    p count_letters(RULES)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When running the below code a type is never returned, despite there being a
Despite the number of similar questions, I haven't yet managed to find any such
I read this question/answer however there wasn't any mention of it being a "bad
Despite primarily being a windows user, I am a huge fan of rsync. Now,
Despite this being one of the best error messages I've ever seen (second only
Whenever I load a Task class, the Document property is always null, despite there
We were having a problem with our build server not checking out modifications from
I think this has been done many times before but despite reading some posts
There have been many threads started over the confusion in the way that Math.Round
There seem to be many different ways of doing OO in JavaScript. I like:

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.