Root directory

Hello,

Right now I need in an application to recurse from the current dir to the 

root dir in ruby. that means eg /home/emmanuel/test, /home/emmanuel, /home,
/.
I’ve been quick to write a Dir.up!() method (i was surprised it’s not
standard?), however i’m having trouble with the Dir.root?() one.
Of course on UNIX it would be pwd == “/” and that’s done, but on windows
that’s another story… with c:, a: and network drives
(\mycomputer\mydrive\mydir)…
I’m quite sure somebody already did that in ruby? I don’t have a windows
computer around at home so it would be hell to get right…

TIA,

emmanuel

···


“Stop the world, I want to hop off!”
–Guy Bedos

actually i think that using something like:

def root?()
path = Dir.pwd
# ugly hack…
Dir.chdir(“…”)
pathUp = Dir.pwd
Dir.chdir(path)
return (pathUp == path)
end

should work on UNIX and windows (only tested under UNIX).
changing temporarily the current directory is extremely ugly (think threads),
but I don’t see any other simple options (if i had a windows machine with
ruby installed and some time however…).

anything better?

emmanuel

···

On Friday 29 November 2002 09:35, Emmanuel Touzery wrote:

Hello,

Right now I need in an application to recurse from the current dir to the
root dir in ruby. that means eg /home/emmanuel/test, /home/emmanuel, /home,
/.
I’ve been quick to write a Dir.up!() method (i was surprised it’s not
standard?), however i’m having trouble with the Dir.root?() one.
Of course on UNIX it would be pwd == “/” and that’s done, but on windows
that’s another story… with c:, a: and network drives
(\mycomputer\mydrive\mydir)…
I’m quite sure somebody already did that in ruby? I don’t have a windows
computer around at home so it would be hell to get right…

TIA,

emmanuel


“Stop the world, I want to hop off!”
–Guy Bedos

Emmanuel Touzery wrote:

I’ve been quick to write a Dir.up!() method (i was surprised it’s not
standard?), however i’m having trouble with the Dir.root?() one.

There’s File#dirname:

irb(main):004:0> File.dirname(‘/usr/local’)
“/usr”

Dunno how well that works with drive letters, net shares, etc. on windows.

I actually found File.split:
irb(main):001:0> File.split(“/home”)
[“/”, “home”]
irb(main):002:0> File.split(“/”)
[“/”, “”]

encouraging. if file.split(dir)[1] == “” it could mean it’s root.

however i don’t know how it behaves under windows, but in linux:
irb(main):003:0> File.split(“c:\test”)
[“.”, “c:\test”]
irb(main):004:0> File.split(“c:\test”)
[“.”, “c:\test”]

:O/

emmanuel

PS: for now i’m using the code i posted earlier. i could quickly check that it
works under windows with standard pathnames (cmd.exe doesn’t support UNC
pathnames, didn’t have the time to check for those). still, it’s really
ugly… :O((

···

On Friday 29 November 2002 22:41, Joel VanderWerf wrote:

Emmanuel Touzery wrote:

I've been quick to write a Dir.up!() method (i was surprised it's not

standard?), however i’m having trouble with the Dir.root?() one.

There’s File#dirname:

irb(main):004:0> File.dirname(‘/usr/local’)
“/usr”

Dunno how well that works with drive letters, net shares, etc. on windows.

Hi,

I actually found File.split:
irb(main):001:0> File.split(“/home”)
[“/”, “home”]
irb(main):002:0> File.split(“/”)
[“/”, “”]

encouraging. if file.split(dir)[1] == “” it could mean it’s root.

No, File#split just returns File#dirname and File#basename, and
File#basename returns “” when the path is ended with “/”.

File.basename(“/home/”) => “”

however i don’t know how it behaves under windows, but in linux:
irb(main):003:0> File.split(“c:\test”)
[“.”, “c:\test”]

Note that “\t” means a tab.

irb(main):004:0> File.split(“c:\test”)
[“.”, “c:\test”]

In 1.7, [“c:”, “test”]. But these behaviors are curious.

File.dirname(“c:”) => “.”
File.dirname(“c:/”) => “c:”
File.dirname(“c:\”) => “c:\”

···

At Sat, 30 Nov 2002 07:48:23 +0900, Emmanuel Touzery wrote:


Nobu Nakada

Hello,

Emmanuel Touzery wrote:

I actually found File.split:
irb(main):001:0> File.split(“/home”)
[“/”, “home”]
irb(main):002:0> File.split(“/”)
[“/”, “”]

encouraging. if file.split(dir)[1] == “” it could mean it’s root.

No, File#split just returns File#dirname and File#basename, and
File#basename returns “” when the path is ended with “/”.

File.basename(“/home/”) => “”

well, if we remove the final “/” it will be ok then?
ok, what about this:

will return “.” if you ask dirUp(root)

def dirUp(path)
# remove final “/” if there is one
cleanPath = path.chomp(File::SEPARATOR)
File.split(cleanPath)[0]
end

def root?(path)
# remove final “/” if there is one
cleanPath = path.chomp(File::SEPARATOR)
File.split(cleanPath)[1] == “”
end

seems to work here under linux…

emmanuel

···

On Saturday 30 November 2002 01:23, nobu.nokada@softhome.net wrote: