Module A extends Moudle B possible?

S

Smart RoR

Hello:

Can Modules extends within themselves?

Like Class A < B is Valid, where B is another class
Module X < Y is invalid, where Y is another module?


Thanks.
 
R

Robert Klemme

Can Modules extends within themselves?

Like Class A < B is Valid, where B is another class
Module X < Y is invalid, where Y is another module?

Not directly. But you can use #included like this:

irb(main):001:0> module A; end
=> nil
irb(main):002:0> module B
irb(main):003:1> def self.included(cl)
irb(main):004:2> cl.class_eval { include A }
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> class X
irb(main):008:1> include B
irb(main):009:1> end
=> X
irb(main):010:0> X.ancestors
=> [X, A, B, Object, Kernel, BasicObject]
irb(main):011:0>

Note however that the inheritance order is reversed. There are a number
of other ways to achieve what you want. The requirement does not seem
to come up very frequently though. So maybe it's not an issue as big as
you think.

Kind regards

robert
 
C

Caleb Clausen

Can Modules extends within themselves?

Like Class A < B is Valid, where B is another class
Module X < Y is invalid, where Y is another module?

Not directly. But you can use #included like this:

irb(main):001:0> module A; end
=> nil
irb(main):002:0> module B
irb(main):003:1> def self.included(cl)
irb(main):004:2> cl.class_eval { include A }
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> class X
irb(main):008:1> include B
irb(main):009:1> end
=> X
irb(main):010:0> X.ancestors
=> [X, A, B, Object, Kernel, BasicObject]
irb(main):011:0>

Note however that the inheritance order is reversed. There are a number
of other ways to achieve what you want. The requirement does not seem
to come up very frequently though. So maybe it's not an issue as big as
you think.

Am I missing something? Why not just include A from B? Like this:

irb(main):001:0> module A; end
=> nil
irb(main):002:0> module B include A end
=> B
irb(main):003:0> class X
irb(main):004:1> include B
irb(main):005:1> end
=> X
irb(main):006:0> X.ancestors
=> [X, B, A, Object, Kernel]
irb(main):007:0>
 
C

Caleb Clausen

In the end, I wanted to see A as ancestor of B.
No classes, only modules.

So, then, you should use include, I think. It's like inheiritance, but
for modules:

module A;end
module B include A end
B.ancestors #=> [B, A]
 
R

Robert Klemme

2010/3/23 Caleb Clausen said:
Am I missing something? Why not just include A from B? Like this:

*I* am the one missing something. I had this nagging feeling that
there must be a simpler way but apparently I was too tired to take it
seriously. Thanks for the correction!

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top