Suppose the variable path is an array of fully qualified path and file
names (e.g., in Windows, an item might be “c:\folder\file.txt”). What
would be an efficient way of creating another array that contained only
the file extensions (one or more characters following the last .
character, if any)? I know I could use a while loop and a counter, but
figure there is a better way in Ruby.
Alternatively, how could I change the array so that it was two
dimensional where each “row” had two “columns” consisting of the full
name and the extension only?
Jamal
On Apr 10, 2006, at 9:26 AM, Jamal M. wrote:
Suppose the variable path is an array of fully qualified path and file
names (e.g., in Windows, an item might be “c:\folder\file.txt”). What
would be an efficient way of creating another array that contained
only
the file extensions (one or more characters following the last .
character, if any)? I know I could use a while loop and a counter,
but
figure there is a better way in Ruby.
files = %w{c:\folder\file1.txt c:\folder\file2.exe c:\folder
\file3.pdf} => [“c:\folder\file1.txt”, “c:\folder\file2.exe”, “c:
\folder\file3.pdf”]
files.map { |f| File.extname(f) }
=> [".txt", “.exe”, “.pdf”]
Alternatively, how could I change the array so that it was two
dimensional where each “row” had two “columns” consisting of the full
name and the extension only?
files.map { |f| e = File.extname(f); [f[0…(-1 - e.length)], e] }
=> [[“c:\folder\file1”, “.txt”], [“c:\folder\file2”, “.exe”], [“c:
\folder\file3”, “.pdf”]]
Hope that helps.
James Edward G. II