Extract methods in a class to mixin?

Discussion in 'Ruby' started by Florian Frank, Sep 10, 2003.

  1. Hello all,

    I just tried to extract the methods defined in a class and make a mixin
    of them. My code looked like this:

    class Class

    def mixin(*methods)
    klass = self
    methods.empty? and methods = klass.instance_methods(false)
    m = Module.new
    m.instance_eval do
    methods.each do |s|
    define_method(s, klass.instance_method(s))
    end
    end
    m
    end

    end

    class A < Array
    def foo; "foo"; end
    def bar; "bar"; end
    end
    class B
    end

    Mixin1 = A.mixin:)foo)
    class B
    include Mixin1
    end
    p B.new.foo

    Mixin2 = A.mixin
    class C
    include Mixin2
    end
    c = C.new
    p c.foo
    p c.bar

    It failed with a type error "bind argument must be an instance of A". I
    don't see why it must be such an instance, because I think this should
    be a job for duck typing to find out if messages fail, that are sent in
    the method body. So i patched eval.c to stop complaining and it worked.
    Perhaps I am missing something: But is there a reason for this check or
    could it be deleted in eval.c to allow this behaviour?

    Index: eval.c
    ===================================================================
    RCS file: /src/ruby/eval.c,v
    retrieving revision 1.531
    diff -u -p -r1.531 eval.c
    --- eval.c 5 Sep 2003 05:07:54 -0000 1.531
    +++ eval.c 8 Sep 2003 12:33:40 -0000
    @@ -7438,10 +7438,6 @@ umethod_bind(method, recv)
    st_lookup(RCLASS(CLASS_OF(recv))->m_tbl, data->oid, 0)) {
    rb_raise(rb_eTypeError, "method `%s' overridden",
    rb_id2name(data->oid));
    }
    - if(!rb_obj_is_kind_of(recv, data->rklass)) {
    - rb_raise(rb_eTypeError, "bind argument must be an instance of %s",
    - rb_class2name(data->rklass));
    - }
    }

    method = Data_Make_Struct(rb_cMethod,struct METHOD,bm_mark,free,bound);

    --
    The whole problem with the world is that fools and fanatics are always so
    certain of themselves, but wiser people so full of doubts.
    -- Bertand Russell
    Florian Frank, Sep 10, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Roth

    Mixin of class methods?

    Michael Roth, Sep 24, 2005, in forum: Ruby
    Replies:
    8
    Views:
    129
    michele
    Sep 26, 2005
  2. Kenneth McDonald
    Replies:
    5
    Views:
    312
    Kenneth McDonald
    Sep 26, 2008
  3. dc

    removing mixin methods

    dc, Oct 31, 2008, in forum: Ruby
    Replies:
    1
    Views:
    74
    Mike Gold
    Oct 31, 2008
  4. James Coglan
    Replies:
    4
    Views:
    101
    ara.t.howard
    Nov 15, 2008
  5. John Lane
    Replies:
    6
    Views:
    183
    John Lane
    Feb 9, 2010
Loading...

Share This Page