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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:47:16+00:00 2026-05-31T11:47:16+00:00

I have a hash of strings navigable_objects = { ‘Dashboard’ => root_path, ‘Timesheets’ =>

  • 0

I have a hash of strings

navigable_objects = { 'Dashboard' => root_path,
                      'Timesheets' => timesheets_path,
                      'Clients' => clients_path,
                      'Projects' => projects_path, 
                    }

I want to convert them into another hash where the key is again the key, but the value is either the string ‘active’ or empty string depending on whether the current controller name contains the key.

For example, lets say that the current controller name is “ClientsController”. The result I should get is:

{ 'Dashboard' => '',
  'Timesheets' => '',
  'Clients' => 'active',
  'Projects' => ''
}

Here is how I am currently doing it:

active = {}

navigable_objects.each do |name, path|
  active[name] = (controller.controller_name.include?(name)) ? 'active' : '')
end 

I feel that while this works, there is a better way to do this in Ruby, possibly using inject or each_with_objects?

  • 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-31T11:47:17+00:00Added an answer on May 31, 2026 at 11:47 am

    NOTE: I already answered but I’m posting this as a separate answer because it’s better for your specific situation, but my other answer still has merit on its own.

    Since you’re not using the values of the hash at all, you can use each_with_object:

    navigable_objects.keys.each_with_object({}) { |k,h| h[k] = controller.controller_name.include?(k) ? 'active' : '' }
    

    Or more verbosely:

    new_hash = navigable_objects.keys.each_with_object({}) do |key, hash|
       hash[key] = controller.controller_name.include?(key) ? 'active' : ''
    end
    

    If your result was based on the values too, then my other solution would work whereas this one wouldn’t.

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

Sidebar

Related Questions

How can my strings have different hash codes but the same text value of
I have a hash table which stores IPs as strings for the key and
I have a Ruby hash with variables: a two-element array of strings an integer
I currently have a legacy database (SQL 2005) that generates hash strings for tokens.
I have a script that utilizes a hash, which contains four strings as keys
I have N strings that I want to divide lexicographic into M even-sized buckets
I'm working on something similiar to GlotPress. I have two tables strings hash (primary
I have a hash of hashes (@post) and I want to save it into
I have an array of strings. Array has length n. How to compute hash
I have a hash filled with 32 strings of the names and locations of

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.