FileTest module confusion

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”)
#---------------------------------------
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
#---------------------------------------
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.

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.

···

On Wed, Aug 14, 2002 at 02:50:28AM +0900, James Toomey wrote:

#---------------------------------------
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…
~

Some modules are meant for mixing in, some aren’t. FileTest isn’t.
Some modules that aren’t designed for mixing in can still be useful when mixed
in, others can’t. FileTest can’t.

I might be wrong :slight_smile:

Anyway, to test the existence of a file, choose your weapon:
FileTest.exists?(path)
File.stat(path).exists?
test ?e, path

Cheers,
Gavin

···

----- Original Message -----
From: “James Toomey” jamesvtoomey@yahoo.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, August 14, 2002 3:50 AM
Subject: FileTest module confusion

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”)
#---------------------------------------
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
#---------------------------------------
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.