Gavin Sinclair requested that I sent this. I hope it makes sense, is
true, and is of use.
Q: I’m working with files and directories on Windows. How do I get a
‘’ (the file separator)?
Whatever I try irb or ‘p’ shows me ‘\’.
use ‘inspect’, which returns
a “displayable” representation of a string that can be re-evalled back
into ruby. [Simon Cozens]
Austin Ziegler notes that you can get away with using forward
slashes (’/’) if you’re only using Ruby file operations; only when
you’re doing Windows-based file operations do you need to do:
foo.gsub('/', '\\')
A (pointless) example:
Dir.entries(“C:ruby/bin”)
or
Dir.entries(“C:/ruby/bin”)
works under Windows, but if you wanted to use the Windows command ‘DIR’
you would have to write Dir C:ruby\\bin
While I’ve been typing, another possible FAQ entry occurs to me:
Q: How do I get a recursive list of all the files under a directory and
its subdirectories? Dir.entries only gets me the files in the specified
directory. Why isn’t there a recursive version?
with directories and ‘files’ with filenames for a given directory
require 'find'
dirs = []
files = []
Find.find(directory) {|f|
if FileTest.directory?(f)
dirs.push(f)
else
files.push(f)
end
}
Please add to/edit/make corrections if needed.
All the best,
Jon
···
A: ‘\’ really does produce ‘’ when your code is run. Both p and irb
A: ‘find’ will probably do what you want. The code below fills ‘dirs’
This message has been checked for all known viruses by Star Internet delivered
through the MessageLabs Virus Control Centre. For further information visit
Any views or personal opinions expressed within this email may not be those of Talis Information Ltd.
The content of this email message and any files that may be attached are confidential, and for the usage of the intended recipient only. If you are not the intended recipient, then please return this message to the sender and delete it. Any use of this e-mail by an unauthorised recipient is prohibited.
Gavin Sinclair requested that I sent this. I hope it makes sense, is
true, and is of use.
Q: I’m working with files and directories on Windows. How do I get a
‘' (the file separator)?
Whatever I try irb or ‘p’ shows me ‘\’.
A: ‘\’ really does produce ‘' when your code is run. Both p and irb
use ‘inspect’, which returns
a “displayable” representation of a string that can be re-evalled back
into ruby. [Simon Cozens]
Austin Ziegler notes that you can get away with using forward
slashes (’/’) if you’re only using Ruby file operations; only when
you’re doing Windows-based file operations do you need to do:
foo.gsub(‘/’, ‘\’)
A (pointless) example:
Dir.entries(“C:ruby/bin”)
or
Dir.entries(“C:/ruby/bin”)
works under Windows, but if you wanted to use the Windows command ‘DIR’
you would have to write Dir C:ruby\\bin
While I’ve been typing, another possible FAQ entry occurs to me:
Q: How do I get a recursive list of all the files under a directory and
its subdirectories? Dir.entries only gets me the files in the specified
directory. Why isn’t there a recursive version?
A: ‘find’ will probably do what you want. The code below fills ‘dirs’
with directories and ‘files’ with filenames for a given directory
require ‘find’
dirs =
files =
Find.find(directory) {|f|
if FileTest.directory?(f)
dirs.push(f)
else
files.push(f)
end
}
Please add to/edit/make corrections if needed.
All the best,
Jon
This message has been checked for all known viruses by Star Internet
delivered through the MessageLabs Virus Control Centre. For further
information visit http://www.star.net.uk/stats.asp
Any views or personal opinions expressed within this email may not be those
of Talis Information Ltd. The content of this email message and any files
that may be attached are confidential, and for the usage of the intended
recipient only. If you are not the intended recipient, then please return
this message to the sender and delete it. Any use of this e-mail by an
unauthorised recipient is prohibited.
–
-A.
Armin Roehrl, http://www.approximity.com
Training, Development and Mentoring
OOP, XP, Java, Ruby, Smalltalk, .Net, Datamining, Parallel computing,
Webservices
How could you do all that?
I did not know it was hard.
–??
Q: How do I get a recursive list of all the files under a directory and
its subdirectories? Dir.entries only gets me the files in the specified
directory. Why isn’t there a recursive version?
A: ‘find’ will probably do what you want. The code below fills ‘dirs’
with directories and ‘files’ with filenames for a given directory
require ‘find’
dirs =
files =
Find.find(directory) {|f|
if FileTest.directory?(f)
dirs.push(f)
else
files.push(f)
end
}
Using “Dir[”#{directory}//“].each” would work just as well as
“Find.find(directory)”.
I don’t know if its any better and it is a little harder to read /
remember. However I find that “Find.find(directory)” in a bit unclear,
it doesn’t really tell you that it will find everything in a particular
directory tree.
To me it suggests that it will find anything that has “directory” in it.
i.e. find . | grep directory (if you where using a *nix console)
My 2p
Rob
-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==---------- http://www.newsfeed.com The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----
I ask some people who post general questions, and get good answers, to
summarise the questions and answers in a new post called “[FAQ]: …”. As far
as I’m concerned, if someone does this, then they’ve made a good contribution
to preserving the knowledge of the commnuity.
Then a smaller group of FAQ maintainers can grab these posts and add them.