Is there any object-oriented File class in ruby ?

G

Gary Wright

Yes, that was indeed the kind of class I was looking for.
Obviously Ruby supports a procedural invocation style of using class
methods on the File class (the same way you would do it with non-oo-
language such as C, i.e. providing the same parameters over and over
again since there is no object that can encapsulate it in a
constructor call).

You are faulting File for not being Pathname but the two classes
serve two different purposes.

File is primarily an interface to the OS system calls, but with some
convenience methods(i.e. basename). Given File you can implement
Pathname but given Pathname you can't implement File.

If File doesn't seem OO enough for you that is probably because the
underlying Posix interface to filesystems isn't really OO either.
That mismatch can be hidden via something like Pathname but at some
point a call has to be passed to the OS and its procedural interface,
which is pretty much what File presents.

I think this is another example where Ruby tends to be pragmatic
rather than pedantic about interfaces. For the simple case of
copying the contents of a file to stdout you could write:

fd = File.new(Pathname.new('/etc/motd'))
while part = fd.read(100)
STDOUT.puts(part)
end
fd.close

or

STDOUT.puts(Pathname.new('/etc/motd').read)

or just

puts File.read('/etc/motd')


I know which one I prefer.

Gary Wright
 
S

s.ross

However, an important difference with ruby File class and the java
File class is that the java class provides a cohesive unit of instance
methods that encapsulates a file path, without having to provide a
string as parameter to every method.

Path name parsing aside -- which is really a small part of what File
does -- here are some comments.

To open and read each line in a file, do this:

File.open(filename, mode) do |f|
f.each {|line| #do something with the line}
end

Notice that the file is opened and a block yielding the context of the
open file is used thereafter for file manipulation. There are a bunch
of filesystem interrogation methods that are, by their nature, better
handled by passing strings. Why? Because often you don't want to open
a file just to find out when it was modified or what its basename is.
That's why there are so many class methods (or, if you prefer, "static
functions").

Encapsulation is about keeping your data in a safe place where the
implementation of the data store is abstracted. Access is through
methods that don't change. It's conceivable that you could create a
class to wrap the File functions like:

class FileTools
def initialize(file_name)
@file_name = file_name
end

def basename
File.basename(@file_name)
end

def atime
File.atime(@file_name)
end

# etc.
end

Then you would need to do this in your code:

f = FileTools.new('my.file')
basename = f.basename
last_accessed = f.atime

I don't typically use a ton of these methods, so specifying the string
each time isn't a huge burden. Creating a new object just so you can
interrogate the filesystem seems like overkill -- it's unlikely the
interface to the filesystem will change any time soon.

Just my $.02
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top