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