From: Andrea Dallera Date: 2010-03-15T23:41:57+09:00 Subject: Re: Overriding new? Hei Chuck, that's exactly what i was trying to avoid. While I'm at it, let me be more specific about my problem, which is actually not just printing out text to the console but changing the state of the newly created instance: class Parent attr_reader :test_value def initialize @test_value = "from parent" end end class Child < Parent def initialize p @test_value end end child = Child.new this should print out "from parent" -- Andrea Dallera http://github.com/bolthar/freightrain http://usingimho.wordpress.com On Mon, 2010-03-15 at 23:32 +0900, Chuck Remes wrote: > On Mar 15, 2010, at 9:13 AM, Andrea Dallera wrote: > > Hi everybody, > > > > given the following situation: > > > > class Parent > > > > def initialize > > print "magic\n" > > end > > > > end > > > > class Child < Parent > > > > def initialize > > print "more magic\n" > > end > > > > end > > > > child = Child.new > > > > is there a way in which i can get "magic" and "more magic" as the > > output, without having to add super to the child class's constructor? I > > was thinking about overriding the class method "new" but i really didn't > > get anything to work up to now. > > Use the #super keyword to call the superclass. > > > class Parent > def initialize > print "magic\n" > end > end > > class Child < Parent > def initialize > super > print "more magic\n" > end > end > > child = Child.new > > ---------- > > cremes$ ruby a.rb > magic > more magic > > cr > >