[FAQ] was RE: How do I get a '\'?

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.

please also post it on the faqtotum.

thx,
-A.

···

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.
–??

Don’t forget File::ALT_SEPARATOR

···

J.Hawkesworth (J.Hawkesworth@talis.com) wrote:

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 ‘\’.


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

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.

Gavin

···

From: “Armin Roehrl” armin@xss.de

please also post it on the faqtotum.
http://www.rubygarden.org/iowa/faqtotum

thx,
-A.

Gavin Sinclair requested that I sent this. I hope it makes sense, is
true, and is of use.

[snip 2 Q&A]