simple counting in Ruby

P

Peter Bailey

Hello,
Can someone please tell me how to get a count for the number of files in
a directory?
Example:
files = Dir.glob("*.pdf")
puts files.count ??

That doesn't work, but, I'd love to know what would work.
Thanks,
Peter
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

Hm, that works for me. And so does: Dir["*.pdf"]
 
P

Phillip Gawlowski

Hello,
Can someone please tell me how to get a count for the number of files in
a directory?
Example:
files = Dir.glob("*.pdf")
puts files.count ??

That doesn't work, but, I'd love to know what would work.

You can interrogate "files" as to what it is, and / or what methods it accepts:
files.class
files.methods.sort

Then you can check Ruby's documentation (or remember off the top of
your head ;) ) what other method would be appropriate (if any).

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.
 
C

Caius Durling

I found my answer. Thanks, anyway. It's "puts files.length."


It'll return an Array, which has #size or #length to return the number =
of elements in it.

C
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top