From: "byroot (Jean Boussier)" Date: 2022-01-27T17:22:46+00:00 Subject: [ruby-core:107306] [Ruby master Feature#18552] Expose `VALUE rb_singleton_class_get(VALUE)` to extensions Issue #18552 has been updated by byroot (Jean Boussier). > Which does seem very clean. I of course meant "doesn't". Oops. > In this case, it's exposing yet another operation unavailable to plain Ruby code. To be fair, I was entertaining the idea to also open a feature request to get a Ruby side API for it, e.g. `Object#has_singletong_class?`. > We should review how much `rb_singleton_class_get()` is exposing and not exposing It really doesn't expose much, it's nearly the same code than `rb_singleton_class` except it returns `Qnil` instead of creating the singleton class on the fly. > whether it's feasible to implement in other runtimes such as TruffleRuby. Not sure about TruffleRuby, but since `msgpack` has a JRuby extension, I also had to do this same there, and their API already allows to check for the singleton class existence without creating it. ---------------------------------------- Feature #18552: Expose `VALUE rb_singleton_class_get(VALUE)` to extensions https://bugs.ruby-lang.org/issues/18552#change-96205 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal ---------------------------------------- Right now the only way to check wether an object has a singleton class is to do something akin to: ```c !SPECIAL_CONST(obj) && FL_TEST(RBASIC(obj)->klass, FL_SINGLETON); ``` Which doesn't seem very clean. ### Use case This came up in `msgpack`. The library have a registry of serializers on a per class basis, and wish to support singleton classes too. So it is calling `rb_singleton_class()` which cause lots of useless singleton classes to be created: https://github.com/msgpack/msgpack-ruby/pull/245 ### Proposed patch https://github.com/ruby/ruby/pull/5499 -- https://bugs.ruby-lang.org/ Unsubscribe: