[Squeak-fr] Ruby et les Traits.
Colin Putney
cputney at wiresong.ca
Tue Oct 23 03:28:06 UTC 2007
On Oct 22, 2007, at 2:44 PM, Ramon Leon wrote:
> Ruby's modules don't allow this level of control. Given modules with
> conflicting methods, you either get all from one or all from the other
> depending on the order you include them. That is my understanding
> anyway.
Ramon is basically correct - the difference is in the way the
conflicts are resolved. If a method is defined in more than one
place, which one is used?
Ruby's modules work by inserting the module into the superclass
chain. The order that modules are included is the order they're
inserted, so by including module later in the sequence you can
override earlier modules the same way a subclass can override a
superclass. For example, the following script prints "a A":
module Uppercase
def alpha
'A'
end
end
module Lowercase
def alpha
'a'
end
end
class Upperfirst
include Uppercase
include Lowercase
end
class Uppersecond
include Lowercase
include Uppercase
end
first = Upperfirst.new
second = Uppersecond.new
puts "#{first.alpha} #{second.alpha}"
Traits, on the other hand works by adding the methods directly into
the class's method dictionary. It can't rely on inheritance to
resolve conflicts, so it provides ways of explicitly resolving
conflicts, as Ramon explained.
Colin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20071022/e9d8c51f/attachment.htm
More information about the Squeak-dev
mailing list
|