From: Robert Klemme Date: 2006-09-25T05:20:14+09:00 Subject: Re: Adding a new method to a class Mickael Faivre-Macon wrote: > Hi, > > What is called the process of adding a new method of an already defined > class ? > > For example: > > class A > def a > end > end > > And in another file: > > class A > def b > end > end > > And now my real question: > > What if I want to declare a new class variable in the initialize method ? > If it's a derived class I can call super: > def initialize > super > @my_var = "" > end > > but if it not derived ? > How can I do this ? You can alias the old initialize method and replace it with another one: irb(main):014:0> class A irb(main):015:1> def initialize(x) irb(main):016:2> @foo = x irb(main):017:2> end irb(main):018:1> end => nil irb(main):019:0> a = A.new 10 => # irb(main):020:0> class A irb(main):021:1> alias initialize_old initialize irb(main):022:1> def initialize(x) irb(main):023:2> initialize_old x irb(main):024:2> @bar = "foo" irb(main):025:2> end irb(main):026:1> end => nil irb(main):027:0> b = A.new 20 => # Kind regards robert