How to detect Module Inclusion for Class Instances (like Module#extend_object)?

Hi all.

Is there a way to tell that a module is being included by a class?
Specifically I want to detect this for instances of the class in question

  • sort of like using #extend_object.

For example,

module Mod
def Mod.extend_object(object)
object.someInstanceFunction
super
end
end

Now “someInstanceFunction” gets called if I do something like…

class Blah
def someInstanceFunction

end
end

object = Blah.new.extend(Mod)

…But, If I do this…

class Blah
include Mod

def someInstanceFunction

end
end

object = Blah.new

… extend_object doesn’t get called (unsurprisingly I guess) - so nor
does “someInstanceFunction” - which is my problem.

Currently using ruby 1.6.6 (2002-01-21) [i686-linux]

Is there a way to call my instance function when the object is
instantiated regardless of whether the module was included by #extend or
by using include?

Cheers,

Martin

···


Martin Hart Tel: +44 (0) 1582 618468
Arnclan Fax: +44 (0) 1582 619596
Union Street, E-mail: martin@zsdfherg.com
Dunstable, Beds LU6 1EX

Martin Hart wrote:

Hi all.

Is there a way to tell that a module is being included by a class?
Specifically I want to detect this for instances of the class in question

  • sort of like using #extend_object.

Take a look at Module#append_features.

regarding Re: How to detect Module Inclusion for Class Instances (like
Module#extend_object)?:

Is there a way to tell that a module is being included by a class?
Specifically I want to detect this for instances of the class in question

  • sort of like using #extend_object.

Take a look at Module#append_features.

AFAICT that works for the class - but not for instances of it…

irb(main):001:0> module P
irb(main):002:1> def P.extend_object(o)
irb(main):003:2> puts “EXTENDED”
irb(main):004:2> end
irb(main):005:1>
irb(main):006:1* def P.append_features(o)
irb(main):007:2> puts “FEATURES APPENDED”
irb(main):008:2> end
irb(main):009:1> end
nil
irb(main):010:0> class A
irb(main):011:1> end
nil
irb(main):012:0>
irb(main):013:0* a = A.new.extend(P)
EXTENDED
#<A:0x401e8628>
irb(main):014:0> class B
irb(main):015:1> include P
irb(main):016:1> end
FEATURES APPENDED
B
irb(main):017:0> a = B.new
#<B:0x401e3088>
irb(main):018:0>

What I want is for the module “P” to do something to instances of B when
they (the instances) are created - not when the class is created. In
other words - all my problems would be solved if extend_object is called
when an instance of B is created. However, it isn’t :slight_smile:

I can see a way to achieve this by using #append_features to create a
B.new function which could then do the work - but this seems a bit ‘arse
about face’ if you’ll excuse the slang :slight_smile:

I have a temporary work around - which is to define a Module function
that creates an instance of the object, and then calls extend on it…

class Factory
def Factory.createB
B.new.extend(P)
end
end

… the problem with this approach is remembering to call it.

I would appreciate any further comments on this - perhaps I am doing
something that I really shouldn’t be trying to do? Trouble is, Ruby is
so cool that it opens avenues I would not even have considered when using
C++ :slight_smile:

TIA

Martin

···

On 12/12/02, 1:08:36 AM, Joel VanderWerf vjoel@PATH.Berkeley.EDU wrote

Martin Hart wrote:

Is there a way to tell that a module is being included by a class?
Specifically I want to detect this for instances of the class in question

  • sort of like using #extend_object.

Take a look at Module#append_features.

AFAICT that works for the class - but not for instances of it…

OK, I see now. Why can’t you just use initialize?

module P
def initialize(*args)
super
puts “Initializing P”
end
end

class B
include P
end

a = B.new

Take a look at Module#append_features.

OK, I see now. Why can’t you just use initialize?

module P
def initialize(*args)
super
puts “Initializing P”
end
end

class B
include P
end

a = B.new

You can, but why on earth do you think it will do what you want?

irb(main):001:0> module X
irb(main):002:1> def initialize(y)
irb(main):003:2> @y = y
irb(main):004:2> end
irb(main):005:1> end
nil
irb(main):006:0> class Y
irb(main):007:1> include X
irb(main):008:1> end
Y
irb(main):009:0> Y.new(6)
#<Y:0x1009b5c8 @y=6>

I’m not sending this to the list, because I may have misunderstood what you
want to do, and don’t have time to fully investigate right now :slight_smile:

Cheers,
Gavin

···

From: “Joel VanderWerf” vjoel@PATH.Berkeley.EDU

regarding Re: How to detect Module Inclusion for Class Instances (like
Module#extend_object)?:

OK, I see now. Why can’t you just use initialize?

The reason that I did’t use initialize was because I had a base class
with a constructor that required arguments. You have solved this with
initialize(*args) on the module (rather than initialize() which is what i
tried using).

Thanks for the help - i’ll do it this way.

Cheers,

Martin

···

On 12/12/02, 8:12:30 PM, Joel VanderWerf vjoel@PATH.Berkeley.EDU wrote

I’m not sending this to the list, because I may have misunderstood what you
want to do, and don’t have time to fully investigate right now :slight_smile:

Whoops!

Gavin “The Idiot” Sinclair

···

From: “Gavin Sinclair” gsinclair@soyabean.com.au