From: "s.ross" Date: 2008-02-20T08:34:40+09:00 Subject: Re: Is there any object-oriented File class in ruby ? On Feb 19, 2008, at 2:45 PM, tom_33 wrote: > For example, the ruby method File.basename is documented as only > supporting forward slashes, regardless of the local file system. > File.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb" > Essentially, this seems to be not much more than a string parsing > function with no OO abstraction that represents a file object. > Compare this with doing the same thing with java: > File f = new File("/home/gumby/work/ruby.rb"); // or new File("C:\ > \home > \\gumby\\work\\ruby.rb"); > f.getName() #=> "ruby.rb" File is an OO abstraction and the fact that you don't see explicit support for alternative path separators does not bear on object orientedness. Consider this: class WinFile < File def self.basename(filename, suffix ='') File.basename(filename.gsub(/\\/, '/'), suffix) end end That allows you to use WinFile everywhere you might have used File, complete with File's attributes, collections, iterators, and so on. It sounds like you are more *used* to Java's implementation of this class. The reason for the "pure" adjective is that everything is derived from a base class, Object. Fixnum, String, File, everything. So anything that works on Object works on all of these. Try this: irb >> 1.public_methods # long list of methods follows >> 1.public_methods - Object.public_methods # shorter list of methods belonging to Fixnum. How do we know? >> 1.class => Fixnum This kind of introspection and single-root object-orientation is much more difficult in more static languages and allows for much of the "magic" of Ruby.