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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:00:30+00:00 2026-06-17T05:00:30+00:00

I have a hash like this: config = { :assets => { :path =>

  • 0

I have a hash like this:

config = {
  :assets => {
    :path => '/assets',
    :aliases => {
      '/assets/' => '/assets/index.html',
      '/assets/index.htm' => '/assets/index.html'
    }
  },
  :api => {
    :path => '/auth/api',
    :aliases => {
      '/auth/api/me' => '/auth/api/whoami'
    }
  }
}

Is it possible to remove duplicates and have config[...][:aliases] assigned in terms of config[...][:path] like "#{dict_im_in()[:path]}/index.html"?

  • 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-17T05:00:31+00:00Added an answer on June 17, 2026 at 5:00 am

    What you are trying to do smells of premature optimization. I’d recommend concentrating on using symbols as much as possible, both as keys and values, in your hash. Symbols are very memory efficient and fast to lookup because Ruby will only create one memory slot for a given symbol, effectively doing what you’re trying to do.

    Your example structure could be rewritten using symbols:

    config = {
      :assets => {
        :path => :'/assets',
        :aliases => {
          :'/assets/' => :'/assets/index.html',
          :'/assets/index.htm' => :'/assets/index.html'
        }
      },
      :api => {
        :path => :'/auth/api',
        :aliases => {
          :'/auth/api/me' => :'/auth/api/whoami'
        }
      }
    }
    

    :'/assets/index.html', no matter where it’s seen, would always point to the same symbol, unlike a string.

    Any time you need to access a value in something expecting a string, do a to_s to it. When you add a value, do a to_sym to it.

    Behind the scenes, you’ll be using memory more efficiently in your Hash, and using what should be the fastest value lookup available in Ruby.

    Now, if you’re using a hash structure to configure your application, and it’s being stored on disk, you can get some space savings using YAML, which supports aliases and anchors inside its data file. They won’t make pointers inside the hash after parsing into a Ruby object, but they can reduce the size of the file.


    EDIT:

    If you need to dynamically define a configuration on the fly, then create variables for all the sub-parts of the various strings, then add a method that generates it. Here’s the basics for dynamically creating such a thing:

    require 'awesome_print'
    
    def make_config(
      assets = '/assets',
      index_html = 'index.html',
      auth_api = '/auth/api'
    )
      assets_index = File.join(assets, index_html)
      {
        :assets => {
          :path => assets,
          :aliases => {
            assets + '/' => assets_index,
            assets_index[0..-2] => assets_index
          }
        },
        :api => {
          :path => auth_api,
          :aliases => {
            File.join(auth_api, 'me') => File.join(auth_api, 'whoami')
          }
        }
      }
    end
    
    ap make_config()
    ap make_config(
      '/new_assets',
      ['new','path','to','index.html'],
      '/auth/new_api'
    )
    

    With what would be generated:

    {
        :assets => {
              :path  => "/assets",
            :aliases => {
                        "/assets/"  => "/assets/index.html",
                "/assets/index.htm" => "/assets/index.html"
            }
        },
          :api => {
              :path  => "/auth/api",
            :aliases => {
                "/auth/api/me" => "/auth/api/whoami"
            }
        }
    }
    

    and:

    {
        :assets => {
              :path  => "/new_assets",
            :aliases => {
                                    "/new_assets/"  => "/new_assets/new/path/to/index.html",
                "/new_assets/new/path/to/index.htm" => "/new_assets/new/path/to/index.html"
            }
        },
          :api => {
              :path  => "/auth/new_api",
            :aliases => {
                "/auth/new_api/me" => "/auth/new_api/whoami"
            }
        }
    }
    

    Edit:

    An alternate way of duplicating value pointers, is to create lambdas or procs that return the information you want. I think of them like they’re function pointers in C or Perl, allowing us to retain the state of a variable that was in scope, or to manipulate the passed-in values. This isn’t a perfect solution, because of variable scoping that occurs, but it’s another tool for the box:

    lambda1 = lambda { 'bar' }
    proc2   = Proc.new { |a,b| a + b }
    proc3   = Proc.new { proc2.call(1,2) }
    
    foo = {
      :key1 => lambda1,
      :key2 => proc2,
      :key3 => proc2,
      :key4 => proc3
    }
    
    foo[:key1].object_id # => 70222460767180
    foo[:key2].object_id # => 70222460365960
    foo[:key3].object_id # => 70222460365960
    foo[:key4].object_id # => 70222460149600
    
    foo[:key1].call          # => "bar"
    foo[:key2].call(1, 2)    # => 3
    foo[:key3].call(%w[a b]) # => "ab"
    foo[:key4].call          # => 3
    

    See “When to use lambda, when to use Proc.new?” for more information on lambdas and procs.

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

Sidebar

Related Questions

I have @hash that looks like this: [1, {:clid=>1, :nvz=>4, :tip=>IP, :name=>Mark, :record=>some text}]
I have a hash that looks like this h1 = {4c09a0da6071a593f051de32=>[4c09a0da6071a593f051de32, Cafe Bistro, 37.78458803130115,
Let's say I have a hash in Ruby like this: d = {1 =>
I have loop and each iteration of Hash looks like this: [1, {:clid=>1, :nvz=>4,
On some of my pages, I have a hash in the url like this:
I have an hash like this: @json = [{id=> 1, username => Example}, {id=>
I have a hash data like this: { current_condition => [ { cloudcover =>
I have Hash like this, representing a data tree hash = { 'key1' =>
I have a hash which looks like this: items: item: attribute_a: cheese attribute_b: bacon
I have a large hash like this: {id=>1, contact_id=>15062422, status=>Complete, [question(12), option(24), piped_page(32] =>

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.