U
Uncutstone Wu
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
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