From: Gary Wright Date: 2008-02-21T07:10:07+09:00 Subject: Re: Is there any object-oriented File class in ruby ? On Feb 20, 2008, at 4:29 PM, tom_33 wrote: > 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