Forum: Ruby Initialization of class when inhereted

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alexander P. (Guest)
on 2009-05-11 13:13
Hi!

class BaseClass
  def BaseClass.initialization_for_derived_class
    @var = do_something
  end
end

class DerivedClass < BaseClass
end

I'd like to initialize @var in DerivedClass. Is there some way to run
initialization_for_derived_class when inheriting BaseClass?
Brian C. (Guest)
on 2009-05-11 13:31
Alexander Ponomarchuk wrote:
> Hi!
>
> class BaseClass
>   def BaseClass.initialization_for_derived_class
>     @var = do_something
>   end
> end
>
> class DerivedClass < BaseClass
> end
>
> I'd like to initialize @var in DerivedClass. Is there some way to run
> initialization_for_derived_class when inheriting BaseClass?

Yes. ri Class#inherited
This topic is locked and can not be replied to.