So, will it be a true mixin , or just a redirection mixin?
The mixin works with the methods and instance variables available on
the bearing instance
module LengthDisplay
def show
puts “Length is #{@len}”
end
end
class One
include LengthDisplay
attr_accessr :len
end
This way One will get a show method from the mixin, but you still can
override the method in the class to get more refined behavior.
It’s not copied - so if you change the method in the module it’s
going to change for all the bearing classes as well.
I dont consider redirection mixins “bad” , its just that they might
require a bit more attention when coding atleast in other
implementations.
are there any risks of identity problems with this in Ruby?
(there sure is in eg. my NAspect framework for .NET where I also use
redirection for mixins… and in that case “this” means the mixin not the
subject)
or are the mixins “proxy aware” so to say, so the “self” pointer inside
a mixin points to the subject and not the mixin?
or are the mixins “proxy aware” so to say, so the “self” pointer
inside
a mixin points to the subject and not the mixin?
Yes, self refers to the subject. Unless you are talking about the own
methods of the module:
module Foo
def self.say
self.something # will call Foo.something
end
def fire
self.ignite! # will call ignite! on the object whose
So, will it be a true mixin , or just a redirection mixin?
You say “true” like non-“redirectrion” is a bad thing. Actually it is a
good thing. Ruby mixins work by using a proxy class which is added to
the inheritance chain, then the mixin module is tied to that proxy
class. The effect is essentially:
class AClass < Mixin1 < Mixin2 … < SuperClass
but it does not require actual subclassing. Eg.
module AMixin
def f; “f”; end
end
class AClass
include AMixin
def f; “f” + super ; end
end
AClass.new.f #=> “ff”
HTH,
T.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.