module include and class re-opening 
Author Message
 module include and class re-opening

Can module2 include module2 and add features to a class from module1?
I tried:

module M1
  class A; def f; "f"; end; end
  class B; def f; "f"; end; end
end

module M2
  include M1
  class A; def g; "g"; end; end  #don't want to subclass
end

M2::A.new.g   # "g", as expected
M2::A.new.f   # undefined method

M2::B::new.f  # "f" as expected

So defining class A in M2 does not 're-open" the included class, but
creates a new class. M1::B, however, comes through fine in M2.

Is it possible
(1) for module M2 to locally modify the definition of M1::A ?
(2) for this modified class to be accessible as M2::A ?

Thanks ...



Tue, 29 Nov 2005 08:21:25 GMT  
 module include and class re-opening

Quote:
> Can module2 include module2 and add features to a class from module1?
> I tried:

> module M1
>   class A; def f; "f"; end; end
>   class B; def f; "f"; end; end
> end

> module M2
>   include M1
>   class A; def g; "g"; end; end  #don't want to subclass
> end

> M2::A.new.g   # "g", as expected
> M2::A.new.f   # undefined method

> M2::B::new.f  # "f" as expected

> So defining class A in M2 does not 're-open" the included class, but
> creates a new class. M1::B, however, comes through fine in M2.

> Is it possible
> (1) for module M2 to locally modify the definition of M1::A ?
> (2) for this modified class to be accessible as M2::A ?

Yes, if you cheat a little:

module M2
   include M1
   M1::A.instance_eval do define_method :g do "G"; end end
end

p M2::A.new.g   # "G"
p M2::A.new.f   # "f"
p M2::B::new.f  # "f"
p M2::A         # M1::A



Tue, 29 Nov 2005 08:36:59 GMT  
 module include and class re-opening

Quote:

> >Can module2 include module2 and add features to a class from module1?
> Yes, if you cheat a little:

> module M2
>   include M1
>   M1::A.instance_eval do define_method :g do "G"; end end
> end

That's cheating a lot :) The following is a little less evil:

Quote:
>> module M1
>> class A; def f; "f"; end; end
>> class B; def f; "f"; end; end
>> end
=> nil
>> module M2
>>  include M1
>>  TheA = M1::A  # could as well do with A
>>  class TheA; def g; "g"; end; end  #don't want to subclass
>> end
=> nil
>> M2::A.new.g   # "g", as expected
=> "g"
>> M2::A.new.f   # works
=> "f"
>> M2::B::new.f  # "f" as expected
=> "f"
>> p M2::TheA

M1::A

But it requires M2 to know about where the classes are coming from, in
case you include several modules (but you normally should, too).

--
 _           _                            
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
        Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Those who don't understand Linux are doomed to reinvent it, poorly.
        -- unidentified source



Tue, 29 Nov 2005 13:45:12 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Module#include and module/class methods

2. grabbing an included module's proxy class

3. Open/Close Reopen in

4. Open/Close Reopen in Cobol (mainframe)

5. including modules => installing modules

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

7. cannot open include file 'param.inc'

8. Problem of comment including an open brace.

9. 62 smalltalk/JAVA and C++ openings...(new positions included 2/24/97)

10. Procedures In window is empty when including debug info in a single module

11. CW2.003 OOP library - Module or Include?

12. NEW FREEWARE : Programming Language Creator by Adept Software (includes a C interpreter module)

 

 
Powered by phpBB® Forum Software