A idea about Ruby module, can we degrade a class to a module

Discussion in 'Ruby' started by Uncutstone Wu, Sep 18, 2006.

  1. To naturally represent following kind of relationship in Ruby code, I
    think, if we can degrade a class to a module, it will be posssible.

    For example, we have a Person class, a Man class ,and a Teacher class.
    Man inherits from Person. And a Man object may also act as a teacher
    。Usually, we can implement this relationship as follows:

    class Person
    end

    class Teacher
    end

    class Man < Person
    set_teacher(a_teacher)
    @teacher = a_teacher
    end
    end

    In this implementation, a Man object is a delegator to a Teacher object.
    To call Teacher methods from a Man object , we need lots of delegate
    method. It’s unconvenient.

    But how about if Ruby provide a degradation mechanism, then we can
    degrade a class to a module, then we can include the degraded class into
    another class. The code will become as follows:

    class Person
    end

    class Teacher
    end

    degrade_to_module Teacher # or something else, this is imagined by me
    Uncutstone Wu, Sep 18, 2006
    #1
    1. Advertising

  2. Uncutstone Wu

    Matt Todd Guest

    > class Person
    > end
    >
    > class Teacher
    > end
    >
    > class Man < Person
    > set_teacher(a_teacher)
    > @teacher = a_teacher
    > end
    > end


    You could always:


    class Person; end

    module Teacher; end

    class Man < Person
    include Teacher
    end


    :D

    So why is it that you need to have Teacher as a class?

    Really, though, I think the best solution would be to do either of the
    following:


    class Person; end

    class Man < Person; end

    class Teacher < Man; end


    or


    class Person; end

    module Teacher; end
    # other modules of things that a Man could be

    class Man < Person; include Teacher; end


    M.T.
    Matt Todd, Sep 18, 2006
    #2
    1. Advertising

  3. Uncutstone Wu

    MonkeeSage Guest

    Uncutstone Wu wrote:
    > But how about if Ruby provide a degradation mechanism, then we can
    > degrade a class to a module, then we can include the degraded class into
    > another class. The code will become as follows:


    Sounds kind of like you want a state pattern. Man is always a Person,
    but only sometimes is a Teacher. So when Man is in the "Teacher state",
    it has all the Teacher methods. Yes, you need some
    deligation/biolerplate code, but I think that is because the
    relationship is not as easy as you imagine. For example, is every
    Teacher a Man, or might some be a Woman? But if some Woman classes are
    already inheriting from Teacher, how will the modularization effect
    them? That's probably not a very clear example, but I think that the
    relationship is not so easy to define since it is not a 1-to-1 map of
    All A are B, but rathetr Some A are B, and some C are B, but no A are
    C. Something like that anyhow. Just ignore me if I make no sense. :)

    Regards,
    Jordan
    MonkeeSage, Sep 18, 2006
    #3
  4. Re: A idea about Ruby module, can we degrade a class to a mo

    I know the example code is not very accurate. Acctually, the last
    snippet of code make more sense. As you said, a man is not always a
    teacher. So, rather than including Teacher into class Man, it's better
    to extend a Man object individually as in the following code.

    class Person
    end
    class Teacher
    end

    degrade_to_module Teacher # or something else, this is imagined by me
    Uncutstone Wu, Sep 18, 2006
    #4
  5. Uncutstone Wu

    Verno Miller Guest

    Re: A idea about Ruby module, can we degrade a class to a mo

    > Uncutstone Wu wrote:
    > ... As you said, a man is not always a teacher. So, rather than including Teacher
    > into class Man, it's better to extend a Man object individually as in the following code.
    >
    > ...
    >
    > Thanks.
    >
    > uncutstone


    ... or you could (somewhat the other way round) have a module Teacher
    that makes a thread_local method available; this way you also could
    dynamically modify class Man!

    http://rubylution.ping.de/articles/2006/05/02/thread-local-variables


    --
    Posted via http://www.ruby-forum.com/.
    Verno Miller, Sep 18, 2006
    #5
  6. Re: A idea about Ruby module, can we degrade a class to a mo

    Verno Miller wrote:
    >> Uncutstone Wu wrote:
    >> ... As you said, a man is not always a teacher. So, rather than including Teacher
    >> into class Man, it's better to extend a Man object individually as in the following code.



    But if I want Teacher to be a class at some other occasions,and sometime
    I want it to be module so that it could be able to be mixed into other
    objects.

    Best regards.

    --
    Posted via http://www.ruby-forum.com/.
    Uncutstone Wu, Sep 18, 2006
    #6
  7. Uncutstone Wu

    Ken Bloom Guest

    Re: A idea about Ruby module, can we degrade a class to a mo

    On Mon, 18 Sep 2006 19:31:20 +0900, Uncutstone Wu wrote:

    > Verno Miller wrote:
    >>> Uncutstone Wu wrote:
    >>> ... As you said, a man is not always a teacher. So, rather than including Teacher
    >>> into class Man, it's better to extend a Man object individually as in the following code.

    >
    >
    > But if I want Teacher to be a class at some other occasions,and sometime
    > I want it to be module so that it could be able to be mixed into other
    > objects.
    >
    > Best regards.
    >


    class Person; end
    class Man < Person; end
    module TeacherModule; end
    class Teacher; include TeacherModule; end

    then you can include TeacherModule into any Man objects you want, and if
    you define all of your Teacher methods on the TeacherModule, then they
    will automatically appear in the teacher class.

    If you still want to look at delegates, have a look at the standard
    Delegator and Forwardable libraries.

    Sadly, I lack more information to tell you whether there's something even
    more clever you can do by taking advantage of duck typing.

    --Ken

    --
    Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
    Ken Bloom, Sep 18, 2006
    #7
  8. Uncutstone Wu

    Trans Guest

    Re: A idea about Ruby module, can we degrade a class to a mo

    Here, you can ditch classes altogether:

    class Module
    def new(*args, &blk)
    mod = self
    klass = Class.new { include mod }
    const_set( "#{name}Class", klass )
    klass.new(*args, &blk)
    end
    end

    Note, this is just off the top of my head so don't expect it to work
    "out-of-the-box" but you get the idea.

    This would be a perfect solution except but there is one limitation:
    the aweful Dynamic Module Inclusion Problem.

    T.
    Trans, Sep 18, 2006
    #8
  9. Uncutstone Wu

    Verno Miller Guest

    Re: A idea about Ruby module, can we degrade a class to a mo

    Trans wrote:
    > Here, you can ditch classes altogether:
    >
    > class Module
    > def new(*args, &blk)
    > mod = self
    > klass = Class.new { include mod }
    > const_set( "#{name}Class", klass )
    > klass.new(*args, &blk)
    > end
    > end
    >
    > ...
    >
    > T.



    Cool, Trans. Reminds me a bit of a snippet that adds virtual methods to
    Ruby.

    http://bigbold.com/snippets/posts/show/2621


    --
    Posted via http://www.ruby-forum.com/.
    Verno Miller, Sep 19, 2006
    #9
  10. Uncutstone Wu

    uncutstone Guest

    Re: A idea about Ruby module, can we degrade a class to a mo

    Thanks for all your replies. they really help.
    uncutstone, Sep 24, 2006
    #10
    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. Replies:
    10
    Views:
    1,220
    Big K
    Feb 2, 2005
  2. Dr Mephesto

    App idea, Any idea on implementation?

    Dr Mephesto, Feb 4, 2008, in forum: Python
    Replies:
    3
    Views:
    702
    Dennis Lee Bieber
    Feb 5, 2008
  3. Anthra Norell
    Replies:
    2
    Views:
    410
    Peter Otten
    Mar 19, 2009
  4. Chris Rebert
    Replies:
    3
    Views:
    380
    Mike Howard
    Mar 28, 2009
  5. Guest
    Replies:
    0
    Views:
    127
    Guest
    Aug 7, 2004
Loading...

Share This Page