From: Jim Freeze Date: 2002-08-14T03:16:26+09:00 Subject: Re: FileTest module confusion On Wed, Aug 14, 2002 at 02:50:28AM +0900, James Toomey wrote: > I'm just learning Ruby, so please forgive any naivete revealed here. > In the following code, > #--------------------------------------- > class DummyClass > include FileTest > end > DummyInstance = DummyClass.new > #This works > puts FileTest.exists?("c:\\autoexec.bat") > #This doesn't work > puts DummyInstance.exists?("c:\\autoexec.bat") Try puts DummyInstance::FileTest.exists?("c:\\autoexec.bat") I believe that the exists? (and family) are module methods. > #--------------------------------------- > it seems like the FileTest module acts more like a class than a > module. From what I've read, modules can be used as "mixins" to > enhance existing classes but cannot be instantiated on their own. > However, the opposite seems to be occurring. > If I substitute the FileTest module for my own module, the expected > behavior occurs: > #--------------------------------------- > module SampleModule > def displayMessage > return "Hello World!" > end > end > class DummyClass2 > include SampleModule > end > DummyInstance2 = DummyClass2.new > #This works > puts DummyInstance2.displayMessage > #This doesn't work > puts SampleModule.displayMessage You can replicate FileTest behaviour with the following: module SampleModule def self.displayMessage return "Hello World!" end end class DummyClass2 include SampleModule end DummyClass2.SampleModule.displayMessage irb(main):010:0> DummyClass2::SampleModule.displayMessage "Hello World!" irb(main):011:0> DummyClass2.displayMessage NameError: undefined method `displayMessage' for DummyClass2:Class from (irb):11 Hope that helps. > #--------------------------------------- > Am I using the FileTest module incorrectly? And if FileTest can be > used as a standalone object (instead of being mixed in), why isn't it > a class instead of a module? I know there's the File::Stat object, but > it seems to operate a bit differently than FileTest. -- Jim Freeze If only I had something clever to say for my comment... ~