Christopher Dicely
2/19/2008 11:53:00 PM
On Feb 19, 2008 2:45 PM, tom_33 <tomjbr.56770318@bloglines.com> wrote:
> I am very new to Ruby and my question is about trying to understand
> why Ruby is often described as a "pure object-oriented language", when
> the core library seems to be quite procedural.
It is a pure object-oriented language because everything is an object,
(even, e.g., things like Fixnum), this contrasts with, e.g., Java,
where certain primitive types are not objects.
>
> For example, the class File seems to have a big bunch of procedural
> functions (or static methods, or whatever you want to call them) that
> needs the filename to be provided as a parameter.
They are called "Class methods" (because, really, they are just
regular instance methods where the receiver happens to be an instance
of class Class.)
And, yes, the class File has a lot of those, because there are a lot
of times when a program will want to get "one shot" information about
a file. You might also note, however, that many of them have
corresponding instance methods either on class File or class
File::Stat, so if you have an existing File instance, so you can do:
File.atime("/home/jsmith/myfile")
or File.new("/home/jsmith/myfile").atime
File.executable?("/home/jsmith/myfile")
or File.new("/home/jsmith/myfile").stat.executable?
Which way is most appropriate in any given use will depend on whether
there is more that you want to do with the file. There is no reason to
create a new file object and keep it around until it gets garbage
collected if you are just doing a one-shot request and don't need to
do anything more with the same file. As is often the case in Ruby,
there is more than one way to do it: and all of them are object
oriented.
>
> 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"
The documentation is incorrect.
irb(main):049:0> VERSION
=> "1.8.6"
irb(main):050:0> File.basename("C:\\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.
Yes, File.basename *is* a string parsing function.
> 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"
Which also seems to be a string parsing function with no necessary OO
abstraction. Handling a variety of path separators is orthogonal to
whether there is OO abstraction going on.
> Now my question is what have I been missing here ?
It seems to me the IO#stat and File#lstat methods, and the File::Stat
class, among other things.