From: Jeffrey Moss Date: 2005-01-04T09:37:32+09:00 Subject: Re: Inheritance of class variables I remember seeing this topic posted to the list a while ago. I'm trying to make a similarly complex model, and its not clear if it is going to work, or if there is a simpler solution for that matter. For the car example I agree that passing the model of the car into the constructor makes sense, but here is my example. I have a class "ActionSequence" for use with rails that has functions "next" and "previous", I want to pass into the constructor the current action and controller, so this is what ActionSequence looks like: class ActionSequence def initialize(action, controller) @action = action @controller = controller end def next action = sequence[ sequence.index(@action) + 1 ] process_action(action) end def previous action = sequence[ sequence.index(@action) - 1 ] process_action(action) end protected def sequence self.class.instance_eval { @sequence } end private def process_action(action) if self.respond_to?(action, true) return self.send(action.id) else return action end end end And an example of a typical usable child class is as follows: class OrderSequence < ActionSequence @@sequence = ['order_selection', 'account_setup', 'personal_information'] def order_selection "error_page" end end So before anything is done, the action will initialize the sequence to the right spot, and inside my "view" I can call next and previous as a helper and it refers to OrderSequence.next and OrderSequence.previous, which traverses the @@sequence class variable, and it will check for a callback function for additional logic. But this doens't work. I haven't been able to figure very much out about how instance_eval works on Class objects. If anybody can offer any advise, or if anybody has done a similar sort of thing, I'd appreciate some input. -Jeff ----- Original Message ----- From: "Robert Klemme" Newsgroups: comp.lang.ruby To: "ruby-talk ML" Sent: Thursday, December 30, 2004 10:16 AM Subject: Re: Inheritance of class variables > > "Eustaquio Rangel de Oliveira Jr." schrieb im > Newsbeitrag news:41D4003E.2060503@yahoo.com... >> Hello there. >> >> I was talking with chris2 and matju on the irc channel and they gave me >> good explanation about this point, but I'd like to share it with you and >> discuss something about it. >> >> I was asking about if, when defined on a parent class, a class variable >> overwrites the class variables with the same name on it's children. > > The short answer is the one that David gave implicite: just don't use > them. IMHO you can safely ignore this language feature without loosing > much but gaining a lot (understandability of code etc.). > > As Guy pointed out, you should really be counting the number of cars in > the individual car classes. That's what he's doing with his instance > variable accesses. > > I would probably model your example completely different, because Honda > and Ford are really instances of CarBuilder not really sub classes. So > this is the result and suddenly we don't need class vars any more: > > class CarBuilder > attr_reader :name, :total_of_cars > > def initialize(name) > @name = name > @total_of_cars = 0 > end > > def build > puts "#{name} builds another car" > @total_of_cars += 1 > end > end > > h = CarBuilder.new "Honda" > f = CarBuilder.new "Ford" > > h.total_of_cars > f.total_of_cars > > h.build > h.build > h.build > f.build > > h.total_of_cars > f.total_of_cars > > > ?> h = CarBuilder.new "Honda" > => # >>> f = CarBuilder.new "Ford" > => # >>> > ?> h.total_of_cars > => 0 >>> f.total_of_cars > => 0 >>> > ?> h.build > Honda builds another car > => 1 >>> h.build > Honda builds another car > => 2 >>> h.build > Honda builds another car > => 3 >>> f.build > Ford builds another car > => 1 >>> > ?> h.total_of_cars > => 3 >>> f.total_of_cars > => 1 > > Regards > > robert > >