|
Re: Sibling rivalry and class variables: msg#01192lang.ruby.general
Hi -- On Tue, 13 Aug 2002, Jim Weirich wrote: > A class instance variable is just a variable that belongs to the class > object rather than an instance of the class. You have to do a little > work to reference a class instance variable from an object of that > class (setting up a class method is sufficient, perhaps there is an > easier way). > > Elaborating on this suggestion, you could do the following ... > > class Parent [...] > def self.show_civ > puts @civ > end > end > > class Son < Parent [...] > def self.show_civ > puts @civ > end > end No need to redefine show_civ in the children, is there? Just for fun, here's a slight variant on your example, using a technique to create the class's instance variable which for some reason I've always found rather neat: class Parent class << self; attr_reader :civ; end @@cv = "Parent Class Variable" @civ = "Parent Class Instance Variable" def show_civ puts type.civ end def show_cv puts @@cv end end class Son < Parent @@cv = "Son Class Variable" @civ = "Son Class Instance Variable" end class Daughter < Parent @@cv = "Daughter Class Variable" @civ = "Daughter Class Instance Variable" end (Uh oh, I might end up back at the self.writer=(x) thread if I go much further... :-) David -- David Alan Black home: dblack@xxxxxxxxxxxxxxxxxxxx work: blackdav@xxxxxxx Web: http://pirate.shu.edu/~blackdav
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: win32ole and excel, Masaki Suketa |
|---|---|
| Next by Date: | Re: Sibling rivalry and class variables, Nikodemus Siivola |
| Previous by Thread: | Re: Sibling rivalry and class variables, Jim Weirich |
| Next by Thread: | Re: Sibling rivalry and class variables, Jim Weirich |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |