[#109844] [Ruby master Feature#18996] Proposal: Introduce new APIs to reline for changing dialog UI colours — "st0012 (Stan Lo)" <noreply@...>

Issue #18996 has been reported by st0012 (Stan Lo).

14 messages 2022/09/07

[#109850] [Ruby master Feature#19000] Data: Add "Copy with changes method" [Follow-on to #16122 Data: simple immutable value object] — "RubyBugs (A Nonymous)" <noreply@...>

Issue #19000 has been reported by RubyBugs (A Nonymous).

42 messages 2022/09/08

[#109905] [Ruby master Bug#19005] Ruby interpreter compiled XCode 14 cannot build some native gems on macOS — "stanhu (Stan Hu)" <noreply@...>

Issue #19005 has been reported by stanhu (Stan Hu).

28 messages 2022/09/15

[#109930] [Ruby master Bug#19007] Unicode tables differences from Unicode.org 14.0 data and removed properties since 13.0 — "nobu (Nobuyoshi Nakada)" <noreply@...>

Issue #19007 has been reported by nobu (Nobuyoshi Nakada).

8 messages 2022/09/17

[#109937] [Ruby master Feature#19008] Introduce coverage support for `eval`. — "ioquatix (Samuel Williams)" <noreply@...>

Issue #19008 has been reported by ioquatix (Samuel Williams).

23 messages 2022/09/17

[#109961] [Ruby master Bug#19012] BasicSocket#recv* methods return an empty packet instead of nil on closed connections — "byroot (Jean Boussier)" <noreply@...>

Issue #19012 has been reported by byroot (Jean Boussier).

8 messages 2022/09/20

[#109985] [Ruby master Feature#19015] Language extension by a heredoc — "ko1 (Koichi Sasada)" <noreply@...>

Issue #19015 has been reported by ko1 (Koichi Sasada).

14 messages 2022/09/22

[#109995] [Ruby master Bug#19016] syntax_suggest is not working with Ruby 3.2.0-preview2 — "hsbt (Hiroshi SHIBATA)" <noreply@...>

Issue #19016 has been reported by hsbt (Hiroshi SHIBATA).

9 messages 2022/09/22

[#110097] [Ruby master Feature#19024] Proposal: Import Modules — "shioyama (Chris Salzberg)" <noreply@...>

SXNzdWUgIzE5MDI0IGhhcyBiZWVuIHJlcG9ydGVkIGJ5IHNoaW95YW1hIChDaHJpcyBTYWx6YmVy

27 messages 2022/09/27

[#110119] [Ruby master Bug#19026] Add `Coverage.supported?(x)` to detect support for `eval` coverage flag. — "ioquatix (Samuel Williams)" <noreply@...>

SXNzdWUgIzE5MDI2IGhhcyBiZWVuIHJlcG9ydGVkIGJ5IGlvcXVhdGl4IChTYW11ZWwgV2lsbGlh

10 messages 2022/09/28

[#110133] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free` — "eightbitraptor (Matthew Valentine-House)" <noreply@...>

SXNzdWUgIzE5MDI4IGhhcyBiZWVuIHJlcG9ydGVkIGJ5IGVpZ2h0Yml0cmFwdG9yIChNYXR0aGV3

8 messages 2022/09/28

[#110145] [Ruby master Misc#19030] [ANN] Migrate lists.ruby-lang.org to Google Groups — "hsbt (Hiroshi SHIBATA)" <noreply@...>

SXNzdWUgIzE5MDMwIGhhcyBiZWVuIHJlcG9ydGVkIGJ5IGhzYnQgKEhpcm9zaGkgU0hJQkFUQSku

12 messages 2022/09/29

[#110154] [Ruby master Bug#19033] One-liner pattern match as Boolean arg syntax error — "baweaver (Brandon Weaver)" <noreply@...>

SXNzdWUgIzE5MDMzIGhhcyBiZWVuIHJlcG9ydGVkIGJ5IGJhd2VhdmVyIChCcmFuZG9uIFdlYXZl

7 messages 2022/09/30

[ruby-core:109897] [Ruby master Feature#12084] `Class#instance`

From: "ufuk (Ufuk Kayserilioglu)" <noreply@...>
Date: 2022-09-15 01:03:19 UTC
List: ruby-core #109897
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/

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

In This Thread

Prev Next