From: "Eregon (Benoit Daloze)" Date: 2022-01-27T21:15:24+00:00 Subject: [ruby-core:107307] [Ruby master Feature#18552] Expose `VALUE rb_singleton_class_get(VALUE)` to extensions Issue #18552 has been updated by Eregon (Benoit Daloze). A new function seems better than that flag check (which doesn't work on TruffleRuby currently, will always return `false`). The name `rb_singleton_class_get()` doesn't seem great though. However I think there is already a way to check this, `RBASIC_CLASS(obj)/rb_class_of(obj)` already gets the singleton_class-if-there-is-one, or the same as `.class`, and there is `Module#singleton_class?` to find out. The first is only available to C exts, the second doesn't seem to have a C API function. But, this is an easy way in C: ``` bool has_singleton_class = rb_class_of(obj) != rb_obj_class(obj); ``` ---------------------------------------- Feature #18552: Expose `VALUE rb_singleton_class_get(VALUE)` to extensions https://bugs.ruby-lang.org/issues/18552#change-96206 * 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: