From: Michael McDaniel Date: 2002-04-11T02:33:52+09:00 Subject: Re: Addition to file.c (File.extension) Mike Hall wrote: > Yukihiro Matsumoto wrote: > >> |YANAGAWA Kazuhisa wrote: >> |> Uh, but that seems too dosish for me.... Nowadays there's so many >> |> people who lives on Windows environments, and they don't doubt `.ext' >> |> is called `extension', but also there's those who are familiar to more >> |> generic `suffix'. > >> |I vote it. > >> "suffix" maybe better than "extension", but it too have same ambiguity. >> Notice it's "File.suffix", not "FileName.suffix". > > This is a fun can of worms! > > Maybe we're going about this the wrong way? > All I want to do is separate a generic file path into: > - directory > - basename > - extension or suffix ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Maybe I'm missing something, but I just do something like this (on UNIX) ... #!/usr/local/bin/ruby def fdir(str) m = /.*\//.match(str) if m != nil; then m.to_s ; else "" ; end end def f(str) m = /.*\//.match(str) if m != nil; then m.post_match ; else "" ; end end def fend(str) m = /.*\./.match(str) if m != nil; then m.post_match ; else "" ; end end # for testing, define environment variable # FTEST=1 before running... if ENV['FTEST']; then filenames = [ "/tmp/fubar/filename", "/tmp/fubar/filename.txt", "/tmp/fubar/filename.txt.bak", "/tmp/fubar foo/filename", "/tmp/fubar foo/filename.txt", "/tmp/fubar foo/filename.txt.bak", "/tmp/fubar foo/some filename", "/tmp/fubar foo/some filename.txt", "/tmp/fubar foo/some filename.txt.bak" ] filenames.each { |f| print (f), ": ", fdir(f), ", ", f(f), ", ", fend(f), "\n" } end # end Of course, you could add the methods to whatever class you wanted... ~mm > > The first two currently exist as clones of UNIX shell commands. > Convenient, but not always useful. > > What if we had a new Ruby method that did what we want? > Maybe a version of File.split which returns [dir, root, suffix]? > Then we don't have to name it! :-) > > (Although File.split always gets me, because I expect an array > of directory components). > > FWIW: Tcl uses 'file dirname', 'file rootname', and 'file extension'. > > -- > Mike Hall > http://www.enteract.com/~mghall