From: Gennady Date: 2003-08-12T08:12:37+09:00 Subject: Re: newbie mixin questions ----- Original Message ----- From: "Michael Garriss" To: "ruby-talk ML" Sent: Monday, August 11, 2003 3:30 PM Subject: Re: newbie mixin questions > Gennady wrote: > > >----- Original Message ----- > >From: "Michael Garriss" > >To: "ruby-talk ML" > >Sent: Monday, August 11, 2003 3:16 PM > >Subject: newbie mixin questions > > > > > > > > > >>Hi, > >> Just 2 questions: > >> > >>1) How can I mixin a class method (not an instance method)? > >> > >> > > > >class A > > class << self > > include SomeModule > > end > >end > > > > > Very cool. Will this require me to have two seperate modules though? > One for including class methods and the other of including the instance > methods. I think so. Technically, you can include the same module both as calss and instance, however usually included methods expect some known environment, either class or instance. You can do some smart checking too, of cource. > > > > > > >>2) How can I force a certain method to be called when the an object of a > >>class that includes that module is initialized? Sort of like a mixin > >>initialize.... > >> > >> > > > >module SomeModule > > def new(*args) > > object = super *args > > # call whatever you want here > > object > > end > >end > > > > Again, very cool. If I include two modules into the same class, both > with a #new, will one be over written? I was not sure untill I tried it, and I was pleasantly surprised that they are called in the order of inclusion. [Ruby is] Very cool ;-) Gennady.