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

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Thu, Dec 23, 2010 at 8:35 AM, Peter Bailey <pbailey@bna.com> 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.
Thanks,
Peter

--
Posted via http://www.ruby-forum.com/\.

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).

···

On Thu, Dec 23, 2010 at 2:35 PM, Peter Bailey <pbailey@bna.com> 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.

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

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

···

--
Posted via http://www.ruby-forum.com/.

files.size or file.length in Ruby 1.8.6

···

On Thu, Dec 23, 2010 at 10:35 AM, Peter Bailey <pbailey@bna.com> 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.
Thanks,
Peter

--
Posted via http://www.ruby-forum.com/\.

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

C

···

On 23 Dec 2010, at 13:40, Peter Bailey wrote:

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

---
Caius Durling
caius@caius.name
+44 (0) 7960 268 100
http://caius.name/