I have a set of methods (related to logging), where the “output device”
for the logging can be either
- A String (representing a file name),
- An object of type File
- An object of type StringIO (logging to a string)
In all places except one, I don’t need to worry about the actual type of
my device object. The exception is, when, at a given point, I need to
“delete the outputfile (if it exists)”.
My first approach was:
File.unlink(device) if File.file?(device)
I hoped that, since StringIO is derived from IO, File::file? would
recognize that this is not a file (since many class methods inside File
can work with arbitrary IO objects).
This is not the case here: File.file? is not defined for a StringIO
I’m using now this workaround:
File.unlink(device) if device.class.name != 'StringIO' &&
I don’t like it, though. Querying the class name always looks like a
Can someone suggest a better way to do it?