Issue #12374 has been updated by Tsuyoshi Sawada.


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 in the ancestors.

Present:

~~~ruby
"".singleton_class.ancestors # => [#<Class:#<String:0x007f3f651d16d0>>, String, ...]
~~~

I want it to be:
~~~ruby
"".singleton_class.ancestors # => [#<Class:#<String:0x007f3f651d16d0>>, SingletonClass, String, ...]
~~~


----------------------------------------
Feature #12374: SingletonClass
https://bugs.ruby-lang.org/issues/12374#change-58782

* Author: Tsuyoshi Sawada
* Status: Feedback
* Priority: Normal
* Assignee: 
----------------------------------------
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:

1. 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`.

2. The way to introduce the singleton pattern using the `Singleton` module (http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html):

~~~ruby
class A
   include Singleton
   # ...
end
~~~

is a bit unnatural and verbose. If we have `SingletonClass`, then we can define a singleton class more naturally:

~~~ruby
A = SingletonClass.new
~~~



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>