From: Brian Candler Date: 2008-10-29T05:31:40+09:00 Subject: Re: Proper way to define a subclass within parent file? Scott Strattner wrote: >> Another possibility, if the values in the instance variables shouldn't >> change >> is to use constants: > > I originally had methods to modify these class variables, but I just > tried using constants, and that worked. So I will probably create a > subclass for every kind of VLAN encountered (which will then define the > constant values for that particular subclass). Note: when referring to constant Foo, use self.class::Foo in a subclass if you want dynamic lookup, otherwise Foo will statically be resolved to the one in the parent. class Bar Foo = 1 def test1 Foo # always refers to Bar::Foo, even in a subclass end def test2 self.class::Foo end end class Baz < Bar Foo = 2 end p Bar.new.test1 # prints 1 p Baz.new.test1 # prints 1 p Bar.new.test2 # prints 1 p Baz.new.test2 # prints 2 -- Posted via http://www.ruby-forum.com/.