H
Helder Ribeiro
This is an aesthetic problem: I have a method in a module, and I want
to include it in three different classes (they're all in the same
file). This method refers itself to one of those classes. So i have to
include the module into the classes, but for the method to be parsed,
one of those classes needs to be already defined.
module ElementFactory
def element_factory(what)
(...)
Element.new
end
end
class Element
include ElementFactory
(...)
end
class Document
include ElementFactory
(...)
end
class ElementCollections
include ElementFactory
(...)
end
So for this class Element there's a circular dependency and, as ruby
does single-pass parsing, this can't work. What I've done so far is to
replace the explicit reference to Element in the module instance
method by a call to Object.const_get
Element) so that when it's
called, Element will already be defined. But this looks a bit ugly.
isn't there a better way to solve this?
The circular dependency hints at not having the module at all and just
putting the instance method element_factory() into Element itself but
there are those other two classes that include it, so I can't do that.
Thanks a lot for any ideas!
Cheers,
Helder
--
http://obvio171.wordpress.com
"Then there were the lonely few of us
who moved back and forth on the strip,
eating rows of beads here and there,
pretending we were Turing machines."
-- xkcd : http://xkcd.com/c205.html
to include it in three different classes (they're all in the same
file). This method refers itself to one of those classes. So i have to
include the module into the classes, but for the method to be parsed,
one of those classes needs to be already defined.
module ElementFactory
def element_factory(what)
(...)
Element.new
end
end
class Element
include ElementFactory
(...)
end
class Document
include ElementFactory
(...)
end
class ElementCollections
include ElementFactory
(...)
end
So for this class Element there's a circular dependency and, as ruby
does single-pass parsing, this can't work. What I've done so far is to
replace the explicit reference to Element in the module instance
method by a call to Object.const_get
called, Element will already be defined. But this looks a bit ugly.
isn't there a better way to solve this?
The circular dependency hints at not having the module at all and just
putting the instance method element_factory() into Element itself but
there are those other two classes that include it, so I can't do that.
Thanks a lot for any ideas!
Cheers,
Helder
--
http://obvio171.wordpress.com
"Then there were the lonely few of us
who moved back and forth on the strip,
eating rows of beads here and there,
pretending we were Turing machines."
-- xkcd : http://xkcd.com/c205.html