Inverse of 'Module#Singleton_Class'

Inverse of `Module#singleton_class`

You can use ObjectSpace#each_object for that:

module M; end
sc = M.singleton_class

ObjectSpace.each_object(Module).find { |m| m.singleton_class == sc }
#=> M

@ndn pointed out that:

ObjectSpace.each_object(sc).to_a #=> [M]

so it's just:

ObjectSpace.each_object(sc).first #=> M

How to find the wrapped class name of a singleton class?

If you want to get the only instance of a singleton class, you can find it in ObjectSpace.

some_singleton_class = some_obj.singleton_class

some_obj_2 = ObjectSpace.each_object(some_singleton_class).first

some_obj_2.object_id == some_obj.object_id #=> true

If your self is a class's singleton class, then the class you're searching for is the only instance of that singleton class.

ObjectSpace.each_object(self).first.name  #=> should return "PaymentRequestx::PaymentRequest"

This approach may not be fast so avoid using it whenever possible.

WARNING: If the class you want has subclasses, then this approach will not work. For example

ObjectSpace.each_object(Object.singleton_class).to_a

will return a huge amount of classes (think why).

UPDATE

You can do further filter from the search result from ObjectSpace.

def instance_of(singleton_class)
ObjectSpace.each_object(singleton_class).find do |obj|
obj.singleton_class == singleton_class
end
end

instance_of(Object.singleton_class) #=> Object

Different between Module Pattern and Singleton Pattern?

Module pattern in javascript refers to the modularisation of code for a common mechanism. It works quite well to split one "class" across several files as you can define constructor and various groups of prototype methods independently. Each of the modules is usually wrapped inside a closure to create static, local variables - this is called revealing module pattern.

Singleton pattern in javascript refers to the restriction of instance creations, often using lazy initialisation.

Of course you can consider the module pattern to be a specialisation of the singleton pattern (see the Wikipedia article), the constructor and its prototype object would take the part of the "single instance" then.

Yet you also could combine them "independently": A module that defines a class which uses the singleton approach.

Why does a python module act like a singleton?

This is the Python Language Reference's description of how importing a module works:

(1) find a module, and initialize it if necessary; (2) define a name or names in the local namespace

(Emphasis added.) Here, initializing a module means executing its code. This execution is only performed if necessary, i.e. if the module was not previously imported in the current process. Since Python modules are first-class runtime objects, they effectively become singletons, initialized at the time of first import.

Note that this means that there's no need for a get_state_dict_code function; just initialize state_code_dict at top-level:

state_code_dict = generate_state_code_dict()

For a more in-depth explanation, see this talk by Thomas Wouters, esp. the first part — around 04:20 — where he discusses the "everything is runtime" principle.

Ruby ancestors method for singleton class objects

I think you'll really like the ruby 2.1 then!

$ rbenv local 2.1.0
roman.brodetski@nb-rbrodetski []
$ irb
...
irb(main):008:0> B.singleton_class.ancestors
=> [#<Class:B>, ClassMethods, #<Class:A>, #<Class:Object>, #<Class:BasicObject>, Class, Module, Object, Kernel, BasicObject]
irb(main):009:0>

I also think it's a great thing that your mind is synchronized with the language developers!

Here is the redmine issue: https://bugs.ruby-lang.org/issues/8035

Creating a singleton in Python

M̲͟͟o̲͟͟s̲͟͟t̲͟͟ ̲͟͟r̲͟͟e̲͟͟c̲͟͟e̲͟͟n̲͟͟t̲͟͟ ̲͟͟v̲͟͟e̲͟͟r̲͟͟s̲͟͟i̲͟͟o̲͟͟n̲͟͟s̲͟͟ ̲͟͟o̲͟͟f̲͟͟ ̲͟͟p̲͟͟y̲͟͟t̲͟͟h̲͟͟o̲͟͟n̲͟͟ ̲͟͟a̲͟͟l̲͟͟r̲͟͟e̲͟͟a̲͟͟d̲͟͟y̲͟͟ ̲͟͟h̲͟͟a̲͟͟v̲͟͟e̲͟͟ ̲͟͟t̲͟͟h̲͟͟e̲͟͟ ̲͟͟d̲͟͟e̲͟͟c̲͟͟o̲͟͟r̲͟͟a̲͟͟t̲͟͟o̲͟͟r̲͟͟ ̲͟͟t̲͟͟h̲͟͟a̲͟͟t̲͟͟ ̲͟͟O̲͟͟P̲͟͟ ̲͟͟w̲͟͟a̲͟͟n̲͟͟t̲͟͟s, It just is not called singleton, since its usage is more generic and meant for functions too:

Python 3.2+

from functools import lru_cache

@lru_cache(maxsize=None)
class CustomClass(object):

def __init__(self, arg):
print(f"CustomClass initialised with {arg}")
self.arg = arg

Usage:

c1 = CustomClass("foo")
c2 = CustomClass("foo")
c3 = CustomClass("bar")

print(c1 == c2)
print(c1 == c3)

Output (notice how foo got printed only once):

>>> CustomClass initialised with foo
>>> CustomClass initialised with bar
>>> True
>>> False

Python 3.9+

from functools.cache

@cache
class CustomClass(object):
...

How can I implement a singleton class in perl?

You can use the Class::Singleton module.

A "Singleton" class can also be easily implemented using either my or state variable (the latter is available since Perl 5.10). But see the @Michael's comment below.

package MySingletonClass;
use strict;
use warnings;
use feature 'state';

sub new {
my ($class) = @_;
state $instance;

if (! defined $instance) {
$instance = bless {}, $class;
}
return $instance;
}


Related Topics



Leave a reply



Submit