From: Eye Matz Date: 2006-10-03T23:50:12+09:00 Subject: Re: Newbie: Question => How to do this with Classes ? unknown wrote: > So, you could do this: > > class Important > def self.set_this_value_method(value) # note the 'self' > end > end > > class SemiImportant < Important > set_this_value_method :x > end Hi David, Thanks for your reply! This was very much the way I was trying to do things and expecting things to work, but when I did it, things did NOT work. Go figure! :( Having taken yours & Jean's replies, I came up with this heavily commented code, WITH lots of questions inside the comments :) ============== # base class that is extended by other classes for shared methods/variables within the # instance object class Important # Not sure if this is really needed, or how to refer/change this to a @@class_variable ?? attr_accessor :my_value # the method that will be used in sub-classes to set the internal value def self.set_this_value_method(value) # note the 'self' # commented out use of class variable, since using a method is better for # possible expansion in the future #@@my_value = value # call to internal method that sets the value to the class variable self.set_my_value(value) end # method that sets/(process) the actuall value def self.set_my_value(value) # using class variable here, but don't understand fully WHY? # should ideally be normal instance variable, but don't know how @@my_value = value # why does not this type of syntax work ? #self.my_value = value end # just dumps the value out def get_my_value @@my_value end def the_value # using the method instead of the variable here "the_value: my value is [#{get_my_value}]" end end # sub-class that is based upon base class class SemiImportant < Important # here we set the internal 'my_value' variable to something set_this_value_method 'something' end # creating a new instance sc = SemiImportant.new # why does this NOT show the my_value variable? # how could I make it show this variable? puts sc.inspect # => # puts sc.get_my_value # => something puts sc.the_value # => the_value: my value is [something] puts sc.my_value # => nil # with this call I can change the value of my_value sc.class.superclass.set_my_value("Don't understand fully") # but I cannot retrieve the value set with this call. WHY?? puts sc.class.superclass.my_value ========== Hopefully someone else that is struggling with the same can find this little snippet and hopefully save themselves some over-worked brain cells. If you (anyone) can answer the included questions I'd love to know the answers. Thanks again. -- Posted via http://www.ruby-forum.com/.