Scope of modules and classes

Hello Ruby-community

I am writing a set of classes to be used in a mod_ruby environment, and
I decided to group them together in a module:

module M
class K1
end

 class K2
 end

 ...

end

Now I am about to write a module M2, that uses some of the classes of
module M:

module M2
class K3 < M::K1
def initialize
@a = M::K2.new
@b = M::K2.new
end
end

 ...

end

So far so good - but then I wanted to get rid of all those "M::"s:

module M2
include M

 class K3 < K1
     def initialize
         @a = K2.new
         @b = K2.new
     end
 end

 ...

end

does not work (K2 is unknown in the scope of K3), and:

module M2
class K3 < M::K1
include M
def initialize
@a = K2.new
@b = K2.new
end
end

 ...

end

seems a little bit ugly to me since I have to include M in every class of M2

Is there a better way, should I stick to the "M::"s, or is it just bad
design that I am doing here?

Thanks in advance,

Bernhard Weitzhofer

Hi –

Hello Ruby-community

I am writing a set of classes to be used in a mod_ruby environment, and
I decided to group them together in a module:
[…]
So far so good - but then I wanted to get rid of all those "M::"s:

module M2
include M

 class K3 < K1
     def initialize
         @a = K2.new
         @b = K2.new
     end
 end

 ...

end

does not work (K2 is unknown in the scope of K3), and:

module M2
class K3 < M::K1
include M
def initialize
@a = K2.new
@b = K2.new
end
end

 ...

end

seems a little bit ugly to me since I have to include M in every class of M2

Is there a better way, should I stick to the "M::"s, or is it just bad
design that I am doing here?

Looking at the design aspect of it, you could put M2 inside M (since
it seems to want to be there :slight_smile: if it makes sense in the larger
picture.

module M
class K1
end

class K2
  def initialize
puts "Initializing a #{self.class}!"
  end
end

module M2
  class K3 < K1
def initialize
  @a = K2.new
end
  end
end

end

include M
M2::K3.new

(For those last two lines you could also do

include M::M2
K3.new

or just

M::M2::K3.new

)

David

···

On Tue, 13 May 2003, Bernhard Weitzhofer wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi

dblack@superlink.net wrote:

Is there a better way, should I stick to the "M::"s, or is it just bad
design that I am doing here?

Looking at the design aspect of it, you could put M2 inside M (since
it seems to want to be there :slight_smile: if it makes sense in the larger
picture.

module M
class K1
end

class K2
  def initialize

puts “Initializing a #{self.class}!”
end
end

module M2
  class K3 < K1

def initialize
@a = K2.new
end
end
end
end

include M
M2::K3.new

The original idea was to build a toolkit (module M). Module M2 should
have only used this toolkit rather than being part of it.

But after some reconsideration, I think it would have been a little
paradox to encapsulate the classes of module M just to virtually give
this encapsulation away again by making them accessible from outside
(from module M2) – so I think I will implement the scheme you suggested
and consider module M2 a “plugin” for toolkit M …

thanks for your help,

Bernhard Weitzhofer