ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:106317] [Ruby master Feature#12084] `Class#instance`
       [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
@ 2021-11-29 18:29 ` Eregon (Benoit Daloze)
  2021-11-29 21:29 ` [ruby-core:106318] " ufuk (Ufuk Kayserilioglu)
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: Eregon (Benoit Daloze) @ 2021-11-29 18:29 UTC (permalink / raw)
  To: ruby-core

Issue #12084 has been updated by Eregon (Benoit Daloze).


`#instance` seems a too generic name for such a rarely-needed meta-programming feature, I think `#singleton_instance` is better.

@justcolin it would result in `Array.singleton_class`, otherwise that would break the invariant of `#singleton_instance` being the reverse of `#singleton_class`.

A concrete use-case would be welcome.

----------------------------------------
Feature #12084: `Class#instance`
https://bugs.ruby-lang.org/issues/12084#change-94944

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature.

~~~RUBY
Array.singleton_class.instance # => Array
"foo".singleton_class.instance # => "foo"
~~~

When the receiver is a class but is not a singleton class, then it should raise an error.

~~~RUBY
Array.instance # => error
~~~



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:106318] [Ruby master Feature#12084] `Class#instance`
       [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
  2021-11-29 18:29 ` [ruby-core:106317] [Ruby master Feature#12084] `Class#instance` Eregon (Benoit Daloze)
@ 2021-11-29 21:29 ` ufuk (Ufuk Kayserilioglu)
  2021-11-30  6:17 ` [ruby-core:106330] " sawa (Tsuyoshi Sawada)
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: ufuk (Ufuk Kayserilioglu) @ 2021-11-29 21:29 UTC (permalink / raw)
  To: ruby-core

Issue #12084 has been updated by ufuk (Ufuk Kayserilioglu).


Agreed that `instance` is a bad name for this concept and we should not be basing the name on the `Singleton` class, since it is not the same kind of singleton in the `singleton_class`.

From what I understand this would be exposing the concept of "attached_object" to user code and that concept is [clearly documented inside the CRuby source code](https://github.com/ruby/ruby/blob/cb4e2cb55a59833fc4f1f6db2f3082d1ffcafc80/include/ruby/internal/module.h#L36) to mean exactly this: 

```
...
 *   - attached object: A singleton class knows its unique instance.
 *     The instance is called the attached object for the singleton class.
...
```

So a good name would be `attached_object` and its behaviour could be:

```ruby
Array.singleton_class.attached_object # => Array
"foo".singleton_class.attached_object # => "foo"

Array.attached_object # => nil
"foo".attached_object # => nil
```

----------------------------------------
Feature #12084: `Class#instance`
https://bugs.ruby-lang.org/issues/12084#change-94945

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature.

~~~RUBY
Array.singleton_class.instance # => Array
"foo".singleton_class.instance # => "foo"
~~~

When the receiver is a class but is not a singleton class, then it should raise an error.

~~~RUBY
Array.instance # => error
~~~



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:106330] [Ruby master Feature#12084] `Class#instance`
       [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
  2021-11-29 18:29 ` [ruby-core:106317] [Ruby master Feature#12084] `Class#instance` Eregon (Benoit Daloze)
  2021-11-29 21:29 ` [ruby-core:106318] " ufuk (Ufuk Kayserilioglu)
@ 2021-11-30  6:17 ` sawa (Tsuyoshi Sawada)
  2021-12-09  7:58 ` [ruby-core:106577] " matz (Yukihiro Matsumoto)
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: sawa (Tsuyoshi Sawada) @ 2021-11-30  6:17 UTC (permalink / raw)
  To: ruby-core

Issue #12084 has been updated by sawa (Tsuyoshi Sawada).


ufuk (Ufuk Kayserilioglu) wrote in #note-4:
> ```ruby
> Array.attached_object # => nil
> "foo".attached_object # => nil
> ```

That clearly cannot be accepted as a feature because you would then have no way to distinguish whether the class's singleton object is `nil` or it does not have a singleton object.

```ruby
NilClass.instance # => nil
```


----------------------------------------
Feature #12084: `Class#instance`
https://bugs.ruby-lang.org/issues/12084#change-94967

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature.

~~~RUBY
Array.singleton_class.instance # => Array
"foo".singleton_class.instance # => "foo"
~~~

When the receiver is a class but is not a singleton class, then it should raise an error.

~~~RUBY
Array.instance # => error
~~~



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:106577] [Ruby master Feature#12084] `Class#instance`
       [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2021-11-30  6:17 ` [ruby-core:106330] " sawa (Tsuyoshi Sawada)
@ 2021-12-09  7:58 ` matz (Yukihiro Matsumoto)
  2021-12-09 10:00 ` [ruby-core:106587] " Eregon (Benoit Daloze)
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: matz (Yukihiro Matsumoto) @ 2021-12-09  7:58 UTC (permalink / raw)
  To: ruby-core

Issue #12084 has been updated by matz (Yukihiro Matsumoto).


* `instance` is NG. For example, `Array.instance => nil` is confusing
* `attached_object` is better, at least for singleton classes. But there's still no real-world use-case.

For your information, `NilClass` is not a singleton class. It's a class with only an instance. This is side evidence of this method is confusing. Even the original proposer can misunderstand the concept.

Matz.


----------------------------------------
Feature #12084: `Class#instance`
https://bugs.ruby-lang.org/issues/12084#change-95240

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature.

~~~RUBY
Array.singleton_class.instance # => Array
"foo".singleton_class.instance # => "foo"
~~~

When the receiver is a class but is not a singleton class, then it should raise an error.

~~~RUBY
Array.instance # => error
~~~



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:106587] [Ruby master Feature#12084] `Class#instance`
       [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2021-12-09  7:58 ` [ruby-core:106577] " matz (Yukihiro Matsumoto)
@ 2021-12-09 10:00 ` Eregon (Benoit Daloze)
  2021-12-14 20:27 ` [ruby-core:106679] " jemmai (Jemma Issroff)
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: Eregon (Benoit Daloze) @ 2021-12-09 10:00 UTC (permalink / raw)
  To: ruby-core

Issue #12084 has been updated by Eregon (Benoit Daloze).


> For your information, `NilClass` is not a singleton class. It's a class with only an instance. This is side evidence of this method is confusing. Even the original proposer can misunderstand the concept.

Interesting, I think what confuses people (including myself) is `nil.singleton_class # => NilClass`.
And basically that's because `#singleton_class` has special behavior for true/false/nil (`special_singleton_class_of`).
Such behavior is not used for any other Ruby value (the rest is TypeError or create a new anonymous singleton class).

It's a bit funny too:
```ruby
irb(main):002:0> TrueClass.singleton_class?
=> false
irb(main):003:0> true.singleton_class.singleton_class?
=> false
```

It kind of makes sense given `TrueClass`, etc are defined as regular classes, and then creating an extra/separate singleton class would be confusing (methods could be defined on either).

I guess it would be possible to create TrueClass as a singleton class (with superclass `Object`) and name it, but that's another proposal and the pros/cons are unclear to me at this stage.

----------------------------------------
Feature #12084: `Class#instance`
https://bugs.ruby-lang.org/issues/12084#change-95251

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature.

~~~RUBY
Array.singleton_class.instance # => Array
"foo".singleton_class.instance # => "foo"
~~~

When the receiver is a class but is not a singleton class, then it should raise an error.

~~~RUBY
Array.instance # => error
~~~



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:106679] [Ruby master Feature#12084] `Class#instance`
       [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2021-12-09 10:00 ` [ruby-core:106587] " Eregon (Benoit Daloze)
@ 2021-12-14 20:27 ` jemmai (Jemma Issroff)
  2021-12-14 20:52 ` [ruby-core:106681] " Eregon (Benoit Daloze)
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: jemmai (Jemma Issroff) @ 2021-12-14 20:27 UTC (permalink / raw)
  To: ruby-core

Issue #12084 has been updated by jemmai (Jemma Issroff).


matz (Yukihiro Matsumoto) wrote in #note-6:
> But there's still no real-world use-case.

We have defined the method described [here](https://github.com/panorama-ed/memo_wise/blob/b20b2cad20f9186c915f2b4a315ef3cfcc0a323a/lib/memo_wise/internal_api.rb#L147-L176) , for MemoWise, a memoization gem. (For what it’s worth, we named it `original_class_from_singleton`.)

It’s feasible that someone wants to memoize a method on a singleton class. Here is an example*:

```
require "memo_wise"

class << String 
  prepend MemoWise

  def example_method
    "example"
  end
  memo_wise :example_method
end

String.example_method
# => “example”
```

Within MemoWise, we receive the `memo_wise(:example_method)` call on the singleton class of `String`, and must resolve it back to define the memoization on the `String` class itself, not its singleton class. We therefore resolve the original class by searching `ObjectSpace`for the class whose singleton class is the one we received:

```
def self.original_class_from_singleton(klass)
  ObjectSpace.each_object(Module).find do |cls|
    cls.singleton_class == klass
  end
end
```

I believe this is almost exactly the built in method being proposed.

*It is worth noting that this same functionality could be achieved by the following snippet, instead of opening up the singleton class:

```
require "memo_wise"

class String 
  prepend MemoWise

  def self.example_method
    "example"
  end
  memo_wise self: :example_method
end

String.example_method
# => "example"
```

But I think part of the beauty of Ruby is that there are multiple ways to express the same sentiment, and while we allow for opening up the singleton class to do this, it makes sense to me that a gem like MemoWise must support this case, and therefore a method as defined in this issue does have a real world use case. It’s also worth noting that a different design decision in MemoWise might make the need go away soon for MemoWise specifically.

----------------------------------------
Feature #12084: `Class#instance`
https://bugs.ruby-lang.org/issues/12084#change-95354

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature.

~~~RUBY
Array.singleton_class.instance # => Array
"foo".singleton_class.instance # => "foo"
~~~

When the receiver is a class but is not a singleton class, then it should raise an error.

~~~RUBY
Array.instance # => error
~~~



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:106681] [Ruby master Feature#12084] `Class#instance`
       [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2021-12-14 20:27 ` [ruby-core:106679] " jemmai (Jemma Issroff)
@ 2021-12-14 20:52 ` Eregon (Benoit Daloze)
  2022-09-15  1:03 ` [ruby-core:109897] " ufuk (Ufuk Kayserilioglu)
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: Eregon (Benoit Daloze) @ 2021-12-14 20:52 UTC (permalink / raw)
  To: ruby-core

Issue #12084 has been updated by Eregon (Benoit Daloze).


jemmai (Jemma Issroff) wrote in #note-8: 
> Within MemoWise, we receive the `memo_wise(:example_method)` call on the singleton class of `String`, and must resolve it back to define the memoization on the `String` class itself, not its singleton class.

That's not clear to me, why do you need to access String? Can't the memoization be stored on the singleton class (String.singleton_class)?
Modules/classes are always "unique", there are no two same instances, so it seems always fine to store the data on the class/module holding the method (the module/class for which `method_defined?(name)` is true/has it in `instance_methods`).

>      #   * Performance concern: searches all Class objects
>      #     But, only runs at load time and results are memoized

BTW, it (internally) iterates all objects on most Ruby implementations, not just modules/classes, the filtering is typically done on top because there is no "next instance pointer" (e.g., unlike in some Smalltalk).
`ObjectSpace.each_object` is a clear no-no when anyone cares about the time to load a library/use it during startup (it's `O(nb of objects in heap at the time of call)`).

----------------------------------------
Feature #12084: `Class#instance`
https://bugs.ruby-lang.org/issues/12084#change-95357

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature.

~~~RUBY
Array.singleton_class.instance # => Array
"foo".singleton_class.instance # => "foo"
~~~

When the receiver is a class but is not a singleton class, then it should raise an error.

~~~RUBY
Array.instance # => error
~~~



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:109897] [Ruby master Feature#12084] `Class#instance`
       [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2021-12-14 20:52 ` [ruby-core:106681] " Eregon (Benoit Daloze)
@ 2022-09-15  1:03 ` ufuk (Ufuk Kayserilioglu)
  2022-09-23 13:19 ` [ruby-core:110046] " Dan0042 (Daniel DeLorme)
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: ufuk (Ufuk Kayserilioglu) @ 2022-09-15  1:03 UTC (permalink / raw)
  To: ruby-core

Issue #12084 has been updated by ufuk (Ufuk Kayserilioglu).


matz (Yukihiro Matsumoto) wrote in #note-6:
> * `attached_object` is better, at least for singleton classes. But there's still no real-world use-case.

I recently ran into a real-world use-case inside one of the gems I maintain and I would like make another case for this request. 

### Use Case - Introspection

To give some context, [Tapioca](https://github.com/Shopify/tapioca) is a gem to generate RBI files for gems and for other DSL generated runtime methods. Tapioca needs to rely heavily on runtime reflection and introspection to achieve what it does so that it can generate a proper RBI representation of what a gem intended to export from its implementation.

One feature we recently added was a way to attribute modules in the ancestor chain of a module/class to the correct gem that the module was mixed in from. In order to do this, we need to hook into the include/extend/prepend operations to keep track of where the mixins are coming from. This part is all working fine, until we get a mixin on the singleton class of a class like:
```ruby
module Foo
end

class Bar
  class << self
    include Foo
  end
end
```
In this case, the mixin is happening on `#<Class:Bar>` but Tapioca needs to match that to one of the ancestors of `Bar` so that it can generate the mixin in the RBI file. In order to do that, Tapioca needs to be able to find `Bar` from `#<Class:Bar>`.

### Workarounds

Our first (naive and failed) attempt to do that was to parse the `to_s` representation of the singleton class to figure out the `Bar` part. This failed, because some classes override the `inspect` method on the class. Most prominently `ActiveRecord::Base` subclasses have [an overridden `inspect` class method](https://github.com/rails/rails/blob/3d1f38fa3066b1e93c7d11d0a2d61885252706b4/activerecord/lib/active_record/core.rb#L370) that has modified output. This ends up changing the output of the `inspect` method on the singleton class, since Ruby makes an internal call to `inspect` on the attached class to get the class name. It is impossible to get the original `inspect` method called via `bind_call` tricks, since the call to the attached class `inspect` method happens inside Ruby.

Reluctantly, we had to resort to the `ObjectSpace` walk solution for matching a singleton class to its attached object (which is a class in our case), which really slowed down our implementation.

Relevant work on Tapioca can be found [here](https://github.com/Shopify/tapioca/pull/1012) and [here](https://github.com/Shopify/tapioca/pull/1098)

### Suggestion

However uncommon, this use-case does not currently have a good workaround, other than resorting to the terrible method of walking the object space for each identification we need to perform.

If this is a compelling use-case for `Class#attached_object` feature, I would love to implement it such that a call to `Class#attached_object` throws if the receiver is not a singleton class to begin with. I think that has the cleanest semantics, since callers can always check with `Module#singleton_class?` before calling `Class#attached_object`.

So the proposed implementation would work like this:
```ruby
# Success cases
Array.singleton_class.attached_object # => Array
Array.singleton_class.singleton_class.attached_object # => #<Class:Array>
"foo".singleton_class.attached_object # => "foo"

# Error cases
Array.attached_object # => error
"foo".attached_object # => error
NilClass.attached_object # => error (since `NilClass.singleton_class? #=> false`)
true.singleton_class.attached_object # => error (since `true.singleton_class.singleton_class? #=> false`)
```

----------------------------------------
Feature #12084: `Class#instance`
https://bugs.ruby-lang.org/issues/12084#change-99140

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature.

~~~RUBY
Array.singleton_class.instance # => Array
"foo".singleton_class.instance # => "foo"
~~~

When the receiver is a class but is not a singleton class, then it should raise an error.

~~~RUBY
Array.instance # => error
~~~



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:110046] [Ruby master Feature#12084] `Class#instance`
       [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2022-09-15  1:03 ` [ruby-core:109897] " ufuk (Ufuk Kayserilioglu)
@ 2022-09-23 13:19 ` Dan0042 (Daniel DeLorme)
  2022-09-23 17:16 ` [ruby-core:110050] " ufuk (Ufuk Kayserilioglu)
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: Dan0042 (Daniel DeLorme) @ 2022-09-23 13:19 UTC (permalink / raw)
  To: ruby-core

Issue #12084 has been updated by Dan0042 (Daniel DeLorme).


I think it would be nicer if `#attached_object` returns nil instead of raising an error. Since nil can't be a valid return value anyway. So you don't need to check for `#singleton_class?` if you want to avoid the cost of Exceptions.

Find all singleton objects:
`ObjectSpace.each_object(Class).filter_map{ |c| c.attached_object rescue nil } `
vs
`ObjectSpace.each_object(Class).filter_map{ |c| c.attached_object if klass.singleton_class? } `
vs
`ObjectSpace.each_object(Class).filter_map(&:attached_object)`


----------------------------------------
Feature #12084: `Class#instance`
https://bugs.ruby-lang.org/issues/12084#change-99296

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature.

~~~RUBY
Array.singleton_class.instance # => Array
"foo".singleton_class.instance # => "foo"
~~~

When the receiver is a class but is not a singleton class, then it should raise an error.

~~~RUBY
Array.instance # => error
~~~



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:110050] [Ruby master Feature#12084] `Class#instance`
       [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
                   ` (8 preceding siblings ...)
  2022-09-23 13:19 ` [ruby-core:110046] " Dan0042 (Daniel DeLorme)
@ 2022-09-23 17:16 ` ufuk (Ufuk Kayserilioglu)
  2022-09-26 22:22 ` [ruby-core:110094] " ufuk (Ufuk Kayserilioglu)
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: ufuk (Ufuk Kayserilioglu) @ 2022-09-23 17:16 UTC (permalink / raw)
  To: ruby-core

Issue #12084 has been updated by ufuk (Ufuk Kayserilioglu).


Dan0042 (Daniel DeLorme) wrote in #note-12:
> I think it would be nicer if `#attached_object` returns nil instead of raising an error. Since nil can't be a valid return value anyway. So you don't need to check for `#singleton_class?` if you want to avoid the cost of Exceptions.

I have no problems with returning `nil` if that is desired, since, as you said, `nil` is never going to be a valid return value. However, I fear that people will see:
```ruby
NilClass.attached_object # => nil
nil.singleton_class # => NilClass 
```
and expect `NilClass.singleton_class?` to be `true`, but it is `false`. For that reason, raising an error felt safer.

Additionally, as I stated above, this is not meant to be an easy to use API for mass consumption, so terseness of the code that uses this method is not my primary concern. As in most reflection related APIs (looking at `const_source_location` and `autoload?`: https://bugs.ruby-lang.org/issues/17354) the usage can be complicated as long as it gets the job done.

@matz Do you have any opinions on the final state of this feature request?

----------------------------------------
Feature #12084: `Class#instance`
https://bugs.ruby-lang.org/issues/12084#change-99301

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature.

~~~RUBY
Array.singleton_class.instance # => Array
"foo".singleton_class.instance # => "foo"
~~~

When the receiver is a class but is not a singleton class, then it should raise an error.

~~~RUBY
Array.instance # => error
~~~



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:110094] [Ruby master Feature#12084] `Class#instance`
       [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
                   ` (9 preceding siblings ...)
  2022-09-23 17:16 ` [ruby-core:110050] " ufuk (Ufuk Kayserilioglu)
@ 2022-09-26 22:22 ` ufuk (Ufuk Kayserilioglu)
  2022-09-27 18:10 ` [ruby-core:110110] " Eregon (Benoit Daloze)
  2022-10-20  4:09 ` [ruby-core:110426] " matz (Yukihiro Matsumoto)
  12 siblings, 0 replies; 13+ messages in thread
From: ufuk (Ufuk Kayserilioglu) @ 2022-09-26 22:22 UTC (permalink / raw)
  To: ruby-core

Issue #12084 has been updated by ufuk (Ufuk Kayserilioglu).


I put together an implementation for this and created a PR: https://github.com/ruby/ruby/pull/6450

----------------------------------------
Feature #12084: `Class#instance`
https://bugs.ruby-lang.org/issues/12084#change-99349

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature.

~~~RUBY
Array.singleton_class.instance # => Array
"foo".singleton_class.instance # => "foo"
~~~

When the receiver is a class but is not a singleton class, then it should raise an error.

~~~RUBY
Array.instance # => error
~~~



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:110110] [Ruby master Feature#12084] `Class#instance`
       [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
                   ` (10 preceding siblings ...)
  2022-09-26 22:22 ` [ruby-core:110094] " ufuk (Ufuk Kayserilioglu)
@ 2022-09-27 18:10 ` Eregon (Benoit Daloze)
  2022-10-20  4:09 ` [ruby-core:110426] " matz (Yukihiro Matsumoto)
  12 siblings, 0 replies; 13+ messages in thread
From: Eregon (Benoit Daloze) @ 2022-09-27 18:10 UTC (permalink / raw)
  To: ruby-core

Issue #12084 has been updated by Eregon (Benoit Daloze).


I'm +1 for adding `Class#attached_object`.
`nil` vs `raise` seem both fine to me, although I slightly prefer `raise` for clarity for the `nil` case.

----------------------------------------
Feature #12084: `Class#instance`
https://bugs.ruby-lang.org/issues/12084#change-99368

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature.

~~~RUBY
Array.singleton_class.instance # => Array
"foo".singleton_class.instance # => "foo"
~~~

When the receiver is a class but is not a singleton class, then it should raise an error.

~~~RUBY
Array.instance # => error
~~~



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:110426] [Ruby master Feature#12084] `Class#instance`
       [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
                   ` (11 preceding siblings ...)
  2022-09-27 18:10 ` [ruby-core:110110] " Eregon (Benoit Daloze)
@ 2022-10-20  4:09 ` matz (Yukihiro Matsumoto)
  12 siblings, 0 replies; 13+ messages in thread
From: matz (Yukihiro Matsumoto) @ 2022-10-20  4:09 UTC (permalink / raw)
  To: ruby-core

Issue #12084 has been updated by matz (Yukihiro Matsumoto).


`attached_object` looks good to me. Accepted.

Matz.

----------------------------------------
Feature #12084: `Class#instance`
https://bugs.ruby-lang.org/issues/12084#change-99736

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature.

~~~RUBY
Array.singleton_class.instance # => Array
"foo".singleton_class.instance # => "foo"
~~~

When the receiver is a class but is not a singleton class, then it should raise an error.

~~~RUBY
Array.instance # => error
~~~



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-10-20  4:09 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-12084.20160218093105.2963@ruby-lang.org>
2021-11-29 18:29 ` [ruby-core:106317] [Ruby master Feature#12084] `Class#instance` Eregon (Benoit Daloze)
2021-11-29 21:29 ` [ruby-core:106318] " ufuk (Ufuk Kayserilioglu)
2021-11-30  6:17 ` [ruby-core:106330] " sawa (Tsuyoshi Sawada)
2021-12-09  7:58 ` [ruby-core:106577] " matz (Yukihiro Matsumoto)
2021-12-09 10:00 ` [ruby-core:106587] " Eregon (Benoit Daloze)
2021-12-14 20:27 ` [ruby-core:106679] " jemmai (Jemma Issroff)
2021-12-14 20:52 ` [ruby-core:106681] " Eregon (Benoit Daloze)
2022-09-15  1:03 ` [ruby-core:109897] " ufuk (Ufuk Kayserilioglu)
2022-09-23 13:19 ` [ruby-core:110046] " Dan0042 (Daniel DeLorme)
2022-09-23 17:16 ` [ruby-core:110050] " ufuk (Ufuk Kayserilioglu)
2022-09-26 22:22 ` [ruby-core:110094] " ufuk (Ufuk Kayserilioglu)
2022-09-27 18:10 ` [ruby-core:110110] " Eregon (Benoit Daloze)
2022-10-20  4:09 ` [ruby-core:110426] " matz (Yukihiro Matsumoto)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).