From: Joel VanderWerf Date: 2002-11-12T06:30:54+09:00 Subject: Re: More fun with modules Eric Schwartz wrote: > Joel VanderWerf writes: > >>Method lookup doesn't go thru enclosing modules, just included modules >>(and superclasses, of course). >> >>However, constant lookup *does* look thru enclosing modules, in >>addition to included modules and superclasses: > > > Ah, that explains it! I was using this facility to define a few > module-level global variables that I didn't want to be /real/ > globals, and it seems to me that the PoLS would indicate that what > works for constants works for other things, too. Why does it work > this way? One answer is: because methods can access the internal state of an object. You want to be able to nest a class within another class, without sharing state. For example: class Space class Point attr_accessor :x, :y def initialize x, y @x = x; @y = y end end attr_accessor :top_left, :bottom_right def initialize x0, y0, x1, y1 @top_left = Point.new x0, y0 @bottom_right = Point.new x1, y1 end end space = Space.new 100, 100, 500, 200 tl = space.top_left p tl.x # ==> 100 # if method lookup for Point fell through to Space, we'd # be able to do this, which is senseless: #p tl.bottom_right