# Simple counting in Ruby

**URL:** https://rubytalk.org/t/simple-counting-in-ruby/61223
**Category:** ruby-talk
**Created:** [23 December 2010 13:35 UTC](https://rubytalk.org/t/simple-counting-in-ruby/61223 "2010-12-23T13:35:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)
#### Post date: [23 December 2010 13:35 UTC](https://rubytalk.org/t/simple-counting-in-ruby/61223/1 "2010-12-23T13:35:33Z")

</div>

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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Andrew\_Wagner](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Andrew\_Wagner](https://rubytalk.org/u/Andrew_Wagner)
#### Post date: [23 December 2010 13:39 UTC](https://rubytalk.org/t/simple-counting-in-ruby/61223/2 "2010-12-23T13:39:42Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)
#### Post date: [23 December 2010 13:40 UTC](https://rubytalk.org/t/simple-counting-in-ruby/61223/3 "2010-12-23T13:40:37Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)
#### Post date: [23 December 2010 13:40 UTC](https://rubytalk.org/t/simple-counting-in-ruby/61223/4 "2010-12-23T13:40:46Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![abinoam](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/abinoam/32/1887_2.png) [@abinoam](https://rubytalk.org/u/abinoam)
#### Post date: [23 December 2010 21:11 UTC](https://rubytalk.org/t/simple-counting-in-ruby/61223/5 "2010-12-23T21:11:15Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Caius\_Durling](https://avatars.discourse-cdn.com/v4/letter/c/edb3f5/32.png) [@Caius\_Durling](https://rubytalk.org/u/Caius_Durling)
#### Post date: [23 December 2010 13:56 UTC](https://rubytalk.org/t/simple-counting-in-ruby/61223/6 "2010-12-23T13:56:49Z")

</div>

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/](http://caius.name/)
