Feature #12374
closed
Added by sawa (Tsuyoshi Sawada) over 8 years ago.
Updated over 8 years ago.
Description
I propose to have a class SingletonClass
, a subclass of the class Class
, to which all singleton classes belong. It should be the owner of all the properties that are specific to singleton classes. Also, the methods defined on Singleton
module should be moved to this class.
Reasons are as follows:
-
I was thinking that the reason #12084 hasn't been seen positively may be because the developers do not want to define a method only on limited instances of a class. If we have SingletonClass
, the method #instance
proposed in #12084 could be defined as an instance method of SingletonClass
.
-
The way to introduce the singleton pattern using the Singleton
module (http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html):
class A
include Singleton
# ...
end
is a bit unnatural and verbose. If we have SingletonClass
, then we can define a singleton class more naturally:
A = SingletonClass.new
Sorry, for the last example, I meant:
A = SingletonClass.new
- Description updated (diff)
- Status changed from Open to Feedback
What is SingletonClass
?
A wrapper object?
def (SingletonClass = Object.new).new(*args)
Class.new(*args) {include Singleton}
end
Nobuyoshi Nakada wrote:
What is SingletonClass
?
A wrapper object?
No, it should be a subclass of Class
. It would perhaps need C-level modification. For any singleton class, I want SingletonClass
to be its class.
Present:
"".singleton_class.class
# => Class
"".singleton_class.class.ancestors
# => [Class, Module, Object, Kernel, BasicObject]
I want it to be:
"".singleton_class.class
# => SingletonClass
"".singleton_class.class.ancestors
# => [SingletonClass, Class, Module, Object, Kernel, BasicObject]
If a singleton class is a subclass of SingletonClass
, no singleton classed inheriting other classes cannot be made.
I think it's a bad idea.
Nobuyoshi Nakada wrote:
If a singleton class is a subclass of SingletonClass
, no singleton classed inheriting other classes cannot be made.
I think it's a bad idea.
Sorry, my previous example was inappropriate. I fixed it. I want a singleton class to be an instance of SingletonClass
.
So...
Object..singleton_class.instance_of?(SingletonClass) #=> true
- Status changed from Feedback to Closed
I don't think the idea itself is meaningless.
But its use-case is not clear to us yet.
(For the record, Singleton module is totally different story.)
If you have any real-world use-case, please reopen the issue.
Matz.
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0