It seems FileTest lacks a `relative?` method (and conversely `absolute?
`). I need such a method. I went to write one but found my self not
100% certain about it being cross-platform. I wrote:
module FileTest
module_function
def relative?(path)
/\A[\/\\]/ !~ File.expand_path(path)
end
end
To put it in English, I expand the path and then simply check for a
leading '/' or '\'.
I noticed Pathname has a `relative?` method and I looked at it. It's
seems a bit bulky:
def relative?
path = @path
while r = chop_basename(path)
path, basename = r
end
path == ''
end
def chop_basename(path)
base = File.basename(path)
if /\A#{SEPARATOR_PAT}?\z/o =~ base
return nil
else
return path[0, path.rindex(base)], base
end
end
private :chop_basename
Is all that really necessary? My version may be too simplistic, but
surely there is more concise way to do this?
Pathname has both #absolute? and #relative?, as well as some other
nice methods for figuring out what a path means. Maybe look there?
Ben
···
On Tue, Dec 21, 2010 at 3:35 PM, Intransition <transfire@gmail.com> wrote:
It seems FileTest lacks a `relative?` method (and conversely `absolute?
`). I need such a method. I went to write one but found my self not
100% certain about it being cross-platform. I wrote:
On Dec 21, 8:35 pm, Intransition <transf...@gmail.com> wrote:
It seems FileTest lacks a `relative?` method (and conversely `absolute?
`). I need such a method. I went to write one but found my self not
100% certain about it being cross-platform. I wrote:
module FileTest
module_function
def relative?\(path\)
/\\A\[\\/\\\\\]/ \!\~ File\.expand\_path\(path\)
end
end
Is all that really necessary? My version may be too simplistic, but
surely there is more concise way to do this?
On Windows, a path without a drive letter that only starts in a single
backslash would be relative (becuase it's relative to the current drive
letter.) A double backslash would be an absolute UNC name.
···
On Wed, 22 Dec 2010 09:06:04 +0900, Intransition wrote:
On Dec 21, 6:52 pm, Luis Lavena <luislav...@gmail.com> wrote:
Your code is not contemplating drive letters.
Good point.
--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/