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

  • Home
  • SEARCH
  • 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 4235398
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:29:39+00:00 2026-05-21T02:29:39+00:00

I have multiple classes that have the following two hash tables. Both mappings must

  • 0

I have multiple classes that have the following two hash tables. Both mappings must have an identical set of keys. My problem is that there are many classes with this structure and that the key sets will change over time.

mapping_1 = {
  :key_1 => "attr_1_1",
  :key_2 => "attr_2_1",
  :key_3 => "attr_3_1"
}

mapping_2 = {
  :key_1 => "attr_1_2",
  :key_2 => "attr_2_2",
  :key_3 => "attr_3_2"
}

I need to be able to access the values given the keys for both mappings and the key from the value with mapping 1. In other words, I need to be able to perform these operations:

mapping_1[:key_1]
mapping_1.index("attr_2_1")  #(Ruby 1.8.7)
mapping_2[:key_3]

Question: Is there a way where I wouldn’t have to duplicate writing the keys in both structures?

I thought of having the key map to an array( :key_1 => ["attr_1_1", "attr_1_2"] ) but this won’t work because when I run the command mapping_1.index() I don’t know what the value of the second attribute is.

  • 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-21T02:29:40+00:00Added an answer on May 21, 2026 at 2:29 am

    I think you should definitely create your own hash class and use it instead of Hash. It would take care of the keyset, and wrap per-object storage.

    It could have class methods to edit your keyset such as:

    def self.add_key(key, default_value)
    def self.remove_key(key)
    

    and instance methods that you would use in your container classes, which would mimick Hash methods:

    def get(key)
    def set(key, value)
    def find_key(value)
    

    etc. You could use ordinary Hash for underlying storage, of course. If you need to be able to expand your keyset whenever you add a new key to any of the instances, just call add_key from set.

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

Sidebar

Related Questions

I have multiple classes that all derive from a base class, now some of
I have a C# singleton class that multiple classes use. Is access through Instance
I have the following two classes: TcmTPDataPanel = class(TcmTPBasePanel) Database: TnxDatabase; Session: TnxSession; private
I have multiple classes and threads that need to write to a Java Swing
  I understand that elements can have multiple classes: .rfrsh-btn { background-image:url(../../upload/rfrsh_nb_grey.png); ... }
My problem is the following; I have a class that uses non-static native methods
I have an enum which is used by multiple classes. What is the best
I have multiple selects: <select id=one> <option value=1>one</option> <option value=2>two</option> <option value=3>three</option> </select> <select
I have multiple threads (C# application running on IIS) running that all need to
I have multiple chunks of code similar to the following: void GetPerson(Action<PersonView, Exception> callback);

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.