From: Anatol Pomozov Date: 2005-08-26T22:12:26+09:00 Subject: Re: Circular dependecy on classes Thanks for your quick answers. Somewhere (i did not remember) I saw following trick def Soldier @can_upgrade_to = [:Knight] #This list != list of all ancestors def upgrade?(to) @can_upgrade_to.include?(to.to_s.to_sym) end end but the solution provided by Derek seems more natural for me. Thanks to all. On 8/26/05, Derek Wyatt wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > > I think you're simply using an instance variable where a method is > what you want. Take advantage of the runtime compile/binding that > you're currently having problems with. Do this instead: > > class Solder > ~ def can_upgrade_to > ~ [Knight] > ~ end > end > > And your problems go away. You don't have to open it up later. If > you want it private, you can simply make it private. > > Regs, > D > > Anatol Pomozov wrote: > | I have several classes that have circular dependencies on each other. > | See following example. > | > | class Solder > | @can_upgrade_to = [Knight] > | #or use constant from Knight class > | > | def upgrade!(to) > | raise "Unable to upgrade" unless @can_upgrade_to.contain(to) > | end > | end > | > | class Knight < Solder > | end > | > | I cant use Knight constant (class name) before Solder and can't use > | Solder before Knight. > | > | I am just come from Java lang and for Java circular class deps is not > | a problem. > | But ruby is a dynamic language and it creates classes just on script > | executing. (right?) > | > | So my questions is: how to solve this problem? What is the best way to > | deal with it. > | > > > - -- > Derek Wyatt - C++ / Ruby / Unix Programmer > http://derekwyatt.org > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.2.4 (MingW32) > > iD8DBQFDDxIfwwHFeC88e2IRAk4TAKDZYh8c9N4fA+yS8noijJinCuYmtACfUmys > 4DjDoOkRivEjy604G24Hqcw= > =gTVJ > -----END PGP SIGNATURE----- > > -- anatol