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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:28:55+00:00 2026-05-29T18:28:55+00:00

For instance, s1 = Student.new(1, Bob, Podunk High) hash[1] = s1 puts hash[1].name #produces

  • 0

For instance,

s1 = Student.new(1, "Bob", "Podunk High")
hash[1] = s1
puts hash[1].name    #produces "Bob"
s1.id = 15
puts hash[15].name   #produces "Bob"
puts hash[1].name    #fails

This is not exactly Hash-like behavior and insertions with the wrong key still needs to be defined.

While I can certainly roll my own container that behaves this way but it will be hard to make it fast, ie not search through the whole container every time [] is called. Just wondering if someone smarter has already made something I can steal.

EDIT: Some good ideas below helped me focus my requirements:

  1. avoid the O(n) lookup time

  2. allow multiple containers to the same object (association not composition)

  3. have different data types (eg. that might use name instead of id) without too much reimplementation

  • 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-29T18:28:57+00:00Added an answer on May 29, 2026 at 6:28 pm

    You can implement it yourself.

    Look at the draft solution:

    class Campus
      attr_reader :students
      def initialize
        @students = []
      end
    
      def [](ind)
        students.detect{|s| s.id == ind}
      end
    
      def <<(st)
        raise "Yarrr, not a student"   if st.class != Student
        raise "We already have got one with id #{st.id}" if self[st.id]
        students << st
      end
    end
    
    class Student
      attr_accessor :id, :name, :prop
      def initialize(id, name, prop)
        @id, @name, @prop = id, name, prop
      end
    end
    
    campus = Campus.new
    st1 = Student.new(1, "Pedro", "Math")
    st2 = Student.new(2, "Maria", "Opera")
    campus << st1
    campus << st2
    campus[1]
    #=> Student...id:1,name:pedro...
    campus[2].name
    #=> Maria
    campus[2].id = 10
    campus[2]
    #=> error
    campus[10].name
    #=> Maria
    

    Or you can play around Array class (or Hash, if you really need it):

    class StrangeArray < Array
      def [](ind)
        self.detect{|v| v.id == ind} || raise "nothing found" # if you really need to raise an error
      end
    
      def <<(st)
        raise "Looks like a duplicate" if self[st.id]
        self.push(st)
      end
    end
    
    campus = StrangeArray.new
    campus << Student.new(15, 'Michael', 'Music')
    campus << Student.new(40, 'Lisa', 'Medicine')
    campus[1]
    #=> error 'not found'
    campus[15].prop
    #=> Music
    campus[15].id = 20
    campus[20].prop
    #=> Music
    

    etc

    And after @tadman’s correct comment you can use reference to your hash right into your Student class:

    class Student
      attr_accessor :name, :prop
      attr_reader :id, :campus
      def initialize(id, name, prop, camp=nil)
        @id, @name, @prop = id, name, prop
        self.campus = camp if camp
      end
    
      def id=(new_id)
        if campus
          rase "this id is already taken in campus" if campus[new_id]
          campus.delete id
          campus[new_id] = self
        end
        @id = new_id
      end
    
      def campus=(camp)
        rase "this id is already taken in campus" if camp[id]
        @campus = camp
        camp[@id] = self
      end
    end
    
    campus = {}
    st1 = Student.new(1, "John", "Math")
    st2 = Student.new(2, "Lisa", "Math", campus)
    # so now in campus is only Lisa
    st1.campus = campus
    # we've just pushed John in campus
    campus[1].name
    #=> John
    campus[1].id = 10
    campus[10].name
    #=> John
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

List<Student> liStudent = new List<Student> { new Student { Name=Mohan,ID=1 }, new Student {
I have class like below template<class T> class Student { public: static Student& Instance();
I have a student class and every student instance created needs to be stored
For instance, this takes 4 lines which is too much space for such a
I have the following block of code: class Student{ int age; //instance variable String
I'm a 2nd year ICT student. I never did PHP before this year and
I have something like this public void FunctionName<T>(){ } and to call this you
instance Monad (Either a) where return = Left fail = Right Left x >>=
For instance, if I have an class: public class StuffHolder { List<Stuff> myList; public
For instance: I have a swf, lets say A, that loads another swf, B.

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.