Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > A idea about Ruby module, can we degrade a class to a module

Reply
Thread Tools

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

 
 
Uncutstone Wu
Guest
Posts: n/a
 
      09-18-2006


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
 
Reply With Quote
 
 
 
 
Matt Todd
Guest
Posts: n/a
 
      09-18-2006
> 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




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.

 
Reply With Quote
 
 
 
 
MonkeeSage
Guest
Posts: n/a
 
      09-18-2006
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

 
Reply With Quote
 
Uncutstone Wu
Guest
Posts: n/a
 
      09-18-2006
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
 
Reply With Quote
 
Verno Miller
Guest
Posts: n/a
 
      09-18-2006
> 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/2...ocal-variables


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Uncutstone Wu
Guest
Posts: n/a
 
      09-18-2006
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/.

 
Reply With Quote
 
Ken Bloom
Guest
Posts: n/a
 
      09-18-2006
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/
 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      09-18-2006
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.


 
Reply With Quote
 
Verno Miller
Guest
Posts: n/a
 
      09-19-2006
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/.

 
Reply With Quote
 
uncutstone
Guest
Posts: n/a
 
      09-24-2006
Thanks for all your replies. they really help.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do deep inheritance trees degrade efficiency? Anthra Norell Python 2 03-19-2009 10:09 AM
Coolpix 4500 - starting to degrade in quality after heavy use niemeyer Digital Photography 12 01-24-2006 08:32 AM
How do I degrade bandwidth or pingflood IP/LINK? KidVoip VOIP 0 01-11-2006 08:01 PM
Is it true that image is degrade after rotation? Editor www.nutritionsoftware.org Digital Photography 11 12-02-2003 12:33 AM
Does software used to print enhance or degrade photo? Ray Hurst Digital Photography 8 11-16-2003 11:51 PM



Advertisments