From: Albert Wagner Date: 2002-01-10T09:54:10+09:00 Subject: Re: How to override subclass#new On Wednesday 09 January 2002 06:14 pm, you wrote: > Albert Wagner writes: > > I have some code similar to below, which expresses my intent, but is > > not what is happening. Who is calling B#initialize? Do I need a > > call to super in B#initialise? I keep getting trash in the b and c > > passed to B#initialize or error messages saying that there are too > > many args being passed to B#initialize. I obviously have a very > > murky understanding of how to achieve what I want. Thanks ahead for > > any advice. > > > > class A > > def A.new(a, *args) > > end > > end > > > > class B < A > > def B.new(a, b, c) > > x = 1 > > y = 2 > > z = 3 > > > > super(a, x, y, z, ) > > end > > def initialize(a, b, c) > > @a = a > > @b = b > > @c = c > > end > > end > > > > B.new(a,b,c) #<==preferred method of creating a B instance > > I am not actually sure what you want, since the only way you show it > is with code that does not work. ;-) But in Ruby, you usually do not > override a class' new method. Instead just override initialize and > call super within initialize. Thank you for the reply, Matt. I am aware of what is usual in Ruby. However, my reasons for doing it are, I think, justified. Note that I am not overriding A#new, but actually attempting to subclass A with B and adding behaviour to A#new through B#new. A#new is left intact to be executed by super in B#new. My problem is that I expected the args a,b and c to be passed from B#new to B#initialize, and they are not. What changes should I make in B to have a,b, and c given to B#initialize? -- Quantum Mechanics: The dreams stuff is made of