From: Robert Klemme Date: 2010-02-11T07:05:15+09:00 Subject: Re: ruby cross-platform File.basename On 02/10/2010 10:44 PM, bwv549 wrote: > What is the best idiom for generating a cross-platform basename? > (i.e., one that will still give the basename of a windows file on > linux and the basename of a linux file on windows). > > filenames = ["C:\\path\\to\\file.txt", "/path/to/file.txt"] > filenames.all? {|fn| (fn) == > "file.txt" } # should be true > > As I play around with this, it seems that on windows (ruby 1.8.7 > (2010-01-10 patchlevel 249) [i386-mingw32]), File.basename will > generate the basename on either of the above examples, but on linux > (ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]) the windows > basename is not found. Based on this, I think this may be a fool- > proof cross-platform idiom for generating the basename: > > File.basename( filename.gsub("\\","/") ) > > This works on windows and linux, at least the versions I'm running. > > Any other/better ideas? Are there any gotchas with the one I'm using > above? What's wrong with using File.basename? Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/