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


nobu (Nobuyoshi Nakada) wrote:
> Seems what you want is (({dup})), not (({__class__})).
> 
>   class Foo < BasicObject
>     mix ::Kernel, dup: :dup, clone: :clone
>   end

But that would include all methods from Kernel with the current behavior of #mix, as `mix ::Kernel` would do.
So you need to opt-out all methods:

    class Foo < BasicObject
      meths = (::Kernel.instance_methods - [:dup])
      mix ::Kernel, meths.each_with_object(dup: :dup) { |m,h| h[m] = nil }
    end

(And Foo.new.dup fails with "undefined method `initialize_dup'")


That behavior of #mix is not very intuitive I think, what do you think about:

diff --git a/class.c b/class.c
index 8e637c0..e9d7a7e 100644
--- a/class.c
+++ b/class.c
@@ -769,8 +769,9 @@ do_mix_method_i(st_data_t key, st_data_t value, st_data_t arg)
     st_table *aliasing = argp->aliasing;
     st_data_t old, alias;

-    if (aliasing && st_lookup(aliasing, ID2SYM(id), &alias)) {
-       if (NIL_P(alias)) return ST_CONTINUE;
+    if (aliasing) {
+       if (!st_lookup(aliasing, ID2SYM(id), &alias) || NIL_P(alias))
+           return ST_CONTINUE;
        id = rb_to_id(alias);
     }
     if (st_lookup(argp->mtbl, id, &old)) {

(and corresponding changes for the three other functions).
That is, if a Hash of methods is given, only import these methods.

----------------------------------------
Feature #6478: BasicObject#__class__
https://bugs.ruby-lang.org/issues/6478#change-26805

Author: trans (Thomas Sawyer)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


How else is one supposed to get the class of a subclass of BasicObject?



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