How to detect Module Inclusion for Class Instances (like Module#extend_object)? 
Author Message
 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

Dunstable, Beds LU6 1EX



Mon, 30 May 2005 08:03:55 GMT  
 How to detect Module Inclusion for Class Instances (like Module#extend_object)?

Quote:

> 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.


Mon, 30 May 2005 09:08:36 GMT  
 How to detect Module Inclusion for Class Instances (like Module#extend_object)?


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

Quote:
> > 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 :-)

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 :-)

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++ :-)

TIA

Martin



Mon, 30 May 2005 09:46:29 GMT  
 How to detect Module Inclusion for Class Instances (like Module#extend_object)?
..

Quote:
>>>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



Tue, 31 May 2005 04:12:30 GMT  
 How to detect Module Inclusion for Class Instances (like Module#extend_object)?

Quote:

> >>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):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)

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 :)

Cheers,
Gavin



Tue, 31 May 2005 10:03:47 GMT  
 How to detect Module Inclusion for Class Instances (like Module#extend_object)?

Quote:

> 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 :)

Whoops!

Gavin "The Idiot" Sinclair



Tue, 31 May 2005 12:13:00 GMT  
 How to detect Module Inclusion for Class Instances (like Module#extend_object)?


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

Quote:
> 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



Tue, 31 May 2005 20:51:04 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Module.extend_object aFixnum

2. Passing module instance names to modules

3. Q: returning python class instance from c-module

4. Module#include and module/class methods

5. About data inclusion by the SCXI module

6. Module inclusion at runtime...

7. Automatic inclusion of modules

8. Automatic inclusion of modules

9. Inclusion of contributed modules

10. Conflict Between Class-as-Module and Class-as-Type (long)

11. instances of classes and instances of instances

12. accessors for module instance vars

 

 
Powered by phpBB® Forum Software