From: Gary Wright Date: 2008-02-20T08:37:22+09:00 Subject: Re: Is there any object-oriented File class in ruby ? On Feb 19, 2008, at 5:45 PM, tom_33 wrote: > Now my question is what have I been missing here ? > I mean, is there any other much better File class somewhere in the > Ruby core that is more pure OO, or otherwise why is a language with a > CORE procedural "class" library described as being a pure oo > language ? It is described as 'pure' because almost all actions are a side- effect of sending a message to an object. In your example File.basename('/dir/file') File is the object, basename is the message, and '/dir/file' is an argument (an instance of String). The most obvious example of the pervasiveness of this pattern is methods on integers: 3.next # 4 3.to_s(2) # "11", 3 as a string of binary digits (-3).abs # 3 the absolute value of -3 (3.1415).floor # float value rounded down to integer Many static syntactical constructs also have a dynamic/method based interface: # subclass A statically class A < B def foo 42 end end # subclass A dynamically class_b = Class.new(A) { def foo 42 end } A big milestone in learning Ruby is to fully understand what it means for a class to be an object at runtime. With regard to the methods you were looking at. If you think of File as an object representing the file system then: File.basename(path) # file system parses path File.open(path) # file system looks up path and returns file instance There is also Pathname, a class for manipulating file path strings and looking up file properties. It provides a unifying interface to other classes such as File, Dir, FileTests, String, and so on. path = Pathname.new("/etc") path.directory? # consults filesystem to see if "/etc" is a directory path.atime # consutls filesystem to get atime of "/ etc" It is also important to realize that Ruby is a practical language in that there is often more than one way to skin a cat with Ruby as opposed to a 'one correct way'. So you can muck around with strings and regular expressions to parse path names or use File or use Pathname or roll your own class. Gary Wright