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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:15:43+00:00 2026-05-28T15:15:43+00:00

This isn’t exactly a singleton, but it’s close, so I imagine it’s common. I

  • 0

This isn’t exactly a singleton, but it’s close, so I imagine it’s common. I have a class (Foo) in which instances correspond to external data structures with unique IDs. I want to ensure that no two instances of Foo can have the same ID – if a constructor is called with the same id value, the original Foo instance with that ID, and all the other values are simply updated. In other words, something like:

class Foo
  def initialize(id, var1, var2)
    if Foo.already_has? id
      t = Foo.get_object(id)
      t.var1 = var1
      t.var2 = var2
      return t
    else
      @var1 = var1
      @var2 = var2
    end
end

I can think of two ways to do this:

  1. I could keep an array of all instances of Foo as a class-level variable, then calling foo_instances.push(self) at the end of the initialization method. This strikes me as kind of ugly.

  2. I believe Ruby already keeps track of instances of each class in some array – if so, is this accessible, and would it be any better than #1?

  3. ??? (Ruby seems to support some slick [meta-]programming tricks, so I wouldn’t be surprised if there already is a tidy way of doing this that I’m missing.

  • 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-28T15:15:43+00:00Added an answer on May 28, 2026 at 3:15 pm

    You can override Foo.new in your object, and do whatever you want in there:

    class Foo
      def self.new(id, var1, var2)
        return instance if instance = self.lookup
        instance = self.allocate
        instance.send :initialize, var1, var2
        return self.store(instance)
      end
    end
    

    You can also, obviously, use a different class method to obtain the object; make the initialize method private to help discourage accidental allocation.

    That way you only have one instance with every ID, which is generally much less painful than the pattern you propose.

    You still need to implement the store and lookup bits, and there isn’t anything better than a Hash or Array in your class to do that with.

    You want to think about wrapping the things you store in your class in a WeakRef instance, but still returning the real object. That way the class can enforce uniqueness without also constraining that every single ID ever used remain in memory all the time.

    That isn’t appropriate to every version of your circumstances, but certainly to some. An example:

      # previous code omitted ...
      return store(instance)
    end
    
    def self.store(instance)
      @instances ||= {}
      @instances[instance.id] = WeakRef.new(instance)
      instance
    end
    
    def self.lookup(id)
      @instances ||= {}
      if weakref = @instances[id] and weakref.weakref_alive?
        return weakref.__getobj__  # the wrapped instance
      else
        return nil
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This isn't exactly exactly a bug because the function still works but if E_ALL
I hope this isn't too simple, but I have a report retrieves rows using
This isn't a real fluent interface. I have an object which builds up a
This isn't legal: public class MyBaseClass { public MyBaseClass() {} public MyBaseClass(object arg) {}
This isn't a holy war, this isn't a question of which is better. What
This isn't a question but I thought I'd start a thread where links to
This isn't my code; I am trying to figure out what exactly this does.
This isn't really a programming question but more about programming and testing tools. Is
This isn't a problem as such, but it's bugging me and I would appreciate
This isn't a very simple question, but hopefully someone has run across it. I

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.