Simple counting in Ruby

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

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

I found my answer. Thanks, anyway. It’s “puts files.length.”

On Thu, Dec 23, 2010 at 2:35 PM, Peter B. [email protected] wrote:

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 :wink: ) what other method would be appropriate (if any).


Phillip G.

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.

On 23 Dec 2010, at 13:40, Peter B. wrote:

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

Caius D.
[email protected]
+44 (0) 7960 268 100
http://caius.name/

files.size or file.length in Ruby 1.8.6