From: "s.ross" Date: 2008-02-21T09:09:42+09:00 Subject: Re: Is there any object-oriented File class in ruby ? On Feb 20, 2008, at 1:29 PM, tom_33 wrote: > 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