[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

File path operations?

Kenneth McDonald

9/22/2008 3:04:00 PM

Ruby has an abstract File/path type which is nice (I'm mainly a Python
user and it doesn't), but it doesn't seem to provide object-oriented
methods to combine file paths, i.e. I can't go path3 = path1/path2 or
something similar. Instead, I have to use the join function, which is
much less elegant, IMHO. Am I missing something. Has someone written
an extension to File that adds such path operations?

Thanks,
Ken

3 Answers

Robert Klemme

9/22/2008 3:09:00 PM

0

2008/9/22 Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net>:
> Ruby has an abstract File/path type which is nice (I'm mainly a Python user
> and it doesn't), but it doesn't seem to provide object-oriented methods to
> combine file paths, i.e. I can't go path3 = path1/path2 or something
> similar. Instead, I have to use the join function, which is much less
> elegant, IMHO. Am I missing something. Has someone written an extension to
> File that adds such path operations?

irb(main):001:0> require 'pathname'
=> true
irb(main):002:0> pn = Pathname.new '.'
=> #<Pathname:.>
irb(main):003:0> pn.absolute?
=> false
irb(main):004:0> pn + "foo" + "bar"
=> #<Pathname:foo/bar>

Cheers

robert


--
use.inject do |as, often| as.you_can - without end

Kenneth McDonald

9/22/2008 3:42:00 PM

0

Thanks!
On Sep 22, 2008, at 10:08 AM, Robert Klemme wrote:

> 2008/9/22 Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net>:
>> Ruby has an abstract File/path type which is nice (I'm mainly a
>> Python user
>> and it doesn't), but it doesn't seem to provide object-oriented
>> methods to
>> combine file paths, i.e. I can't go path3 = path1/path2 or something
>> similar. Instead, I have to use the join function, which is much less
>> elegant, IMHO. Am I missing something. Has someone written an
>> extension to
>> File that adds such path operations?
>
> irb(main):001:0> require 'pathname'
> => true
> irb(main):002:0> pn = Pathname.new '.'
> => #<Pathname:.>
> irb(main):003:0> pn.absolute?
> => false
> irb(main):004:0> pn + "foo" + "bar"
> => #<Pathname:foo/bar>
>
> Cheers
>
> robert
>
>
> --
> use.inject do |as, often| as.you_can - without end
>


Mike Gold

9/22/2008 6:07:00 PM

0


I went for years without knowing about Pathname. It seems rather
underused.

The singleton methods File.basename, File.exist?, etc. are awkward
because the proper abstraction is a path, not an arbitrary string.
Moreover, these methods cut against the natural flow of ruby code.
Compare

File.join File.dirname(foo), "intermediate", File.basename(foo)

with

foo.dirname.join "intermediate", foo.basename

I would like to see more ruby packages use Pathname. If your method
takes a string argument, then use it as a string. However if your
method takes a path name disguised as a string, consider taking a
Pathname. (In practice I expect you'd convert any non-Pathnames to
Pathnames.)

Now that Pathname has been in the standard library since 1.8.0, perhaps
it is time for the whole standard library to use it? It certainly makes
code cleaner and clearer.

It also discourages the use of problematic "#{x}/#{y}" constructs. If x
is nil, you are now pointing at the root directory, with not a peep of
warning from ruby.
--
Posted via http://www.ruby-....