From: chad fowler Date: 2000-12-29T04:40:09+09:00 Subject: [ruby-talk:8190] Re: Scope of "include" -- craig duncan wrote: > I was suprised to see that it doesn't matter where > you "include" a > module in a source file. I saw some code posted > that included the > module before a class definition but implied that > the class was picking > up the methods defined by the module. This strikes > me as odd. This > means you have no control over where the module > methods and constants > go. Suppose you define several classes in a single > file . . . then > every class picks up what's in the module, rather > than doing the > "include" within the scope of the class that you > want to pick it up. > Has this struck anyone else as strange (and perhaps > undesirable) and is > there some logic behind it? > You can put the "include" inside the class definition to limit its scope to the class. def MyModule def myMessage print "Hello from my message" end end def MyClass include MyModule end def MyOtherClass end a = MyClass a.myMessage # works b = MyOtherClass b.myMessage # fails --Chad __________________________________________________ Do You Yahoo!? Yahoo! Photos - Share your holiday photos online! http://photos.yahoo.com/