This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: The Pathname crusade continues
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
Just when I thought I was nearly ready to present a revamped Pathname class, I've hit a snag. It rests with the concept of a path root.
My oh-so-clever original implementation just used File.dirname until it eventually worked down to the smallest string component. That would be the root. It works, too. So, the root of "/foo/bar" is "/" while the root of "C:\foo\bar" is "C:\". That's great, except for the part where it doesn't work on UNC paths.
You see, if you're on Windows and have a mapped drive, it looks something like "\\foo\bar\baz". Go ahead, take a guess at what the root of that path is. Did you guess "\\"? Or "\\foo"? You guessed wrong. The root of that path is "\\foo\bar".
So, how does one go about creating a sane way of determining the root directory in a cross platform way? The answer is, you don't, at least not without jumping through a lot of potentially flawed hoops. I'd rather avoid that. Fortunately, there *is* a solution in the Win32 API which is kind enough to provide some path name handling methods. The method in particular is called PathStripToRoot. It has some other handy methods I plan to use as well.
In other news, I'm glad to see that my revamped version of Pathname#cleanpath actually matches what the PathCanonicalize method does. The current implementation doesn't make any sense to me. So, that's a bit of ammo for me for anyone who complains. :)