# How to group visitors using hours

**URL:** <https://www.ruby-forum.com/t/how-to-group-visitors-using-hours/161782>\
**Category:** Ruby\
**Created:** [March 2, 2009, 1:13pm UTC](https://www.ruby-forum.com/t/how-to-group-visitors-using-hours/161782 "2009-03-02T13:13:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brazov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@brazov](https://www.ruby-forum.com/u/brazov)\
**Post date:** [March 2, 2009, 1:13pm UTC](https://www.ruby-forum.com/t/how-to-group-visitors-using-hours/161782/1 "2009-03-02T13:13:36Z")

</div>

Hi,

I’ve got this problem, I have a Visitor class that keep tracks  
of visitors. Each visitor has “created\_at” field.  
I’d like to group them by hours using “created\_at”

I came up with this solution, but I’m not very happy about it…

First, I create a list having [“yy-mm-dd hh”,visitor.id], with

```
list = visitors.map{|i| [i.created_at.strftime("%Y-%m-%d %H"),

```

i.id]}

Then, I group each visitor depending on the hours using an Hash

hs= {}  
for l in list  
if hs.has\_key?(l.first)  
hs[l.first] \<\< l.second # Use the hour as index, and append the  
visitor id  
else  
hs[l.first] = [l.second]  
end  
end

The result is a Hash hs, and I can use it to get the visitors  
given a certain hour.

However, I’m sure there’s a better way of doing it, especially if  
I’d like to group visitors for “months” or “years”

Any suggestions please?!  
thanks  
b.

---

<div class="post-metadata">

**Author:** ![brazov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@brazov](https://www.ruby-forum.com/u/brazov)\
**Post date:** [March 2, 2009, 1:31pm UTC](https://www.ruby-forum.com/t/how-to-group-visitors-using-hours/161782/2 "2009-03-02T13:31:03Z")

</div>

On Mon, Mar 2, 2009 at 9:12 PM, Ayeye B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## Â Â list = visitors.map{|i| [i.created\_at.strftime(“%Y-%m-%d %H”), Â Â Â Â hs[l.first] = [l.second] thanks b.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

require ‘pp’

class Visitor  
attr\_accessor :created\_at

def initialize(time)  
@created\_at = time  
end  
end

visitors = Array.new(10){ Visitor.new(Time.at(rand(2e9))) }

pp visitors.group\_by{|v| v.created\_at.hour }  
{6=\>  
[#\<Visitor:0xb7bc034c @created\_at=Tue Jul 04 06:22:51 +0900 1978\>,  
#\<Visitor:0xb7bc00a4 @created\_at=Thu Jul 26 06:27:32 +0900 1979\>],  
18=\>  
[#\<Visitor:0xb7bc0194 @created\_at=Wed Jun 27 18:31:17 +0900 2018\>,  
#\<Visitor:0xb7bbffdc @created\_at=Sun Oct 03 18:37:50 +0900 1993\>],  
13=\>[#\<Visitor:0xb7bc0108 @created\_at=Wed Feb 10 13:20:34 +0900 2027\>],  
2=\>  
[#\<Visitor:0xb7bc0158 @created\_at=Fri Jan 10 02:22:01 +0900 2003\>,  
#\<Visitor:0xb7bc0054 @created\_at=Sun May 30 02:15:58 +0900 1993\>],  
9=\>[#\<Visitor:0xb7bbff8c @created\_at=Tue Oct 05 09:32:10 +0900 2032\>],  
4=\>[#\<Visitor:0xb7bc0388 @created\_at=Mon May 01 04:03:29 +0900 2006\>],  
10=\>[#\<Visitor:0xb7bc002c @created\_at=Mon Jun 27 10:28:29 +0900 1994\>]}

^ manveru

---

<div class="post-metadata">

**Author:** ![brazov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@brazov](https://www.ruby-forum.com/u/brazov)\
**Post date:** [March 2, 2009, 3:39pm UTC](https://www.ruby-forum.com/t/how-to-group-visitors-using-hours/161782/3 "2009-03-02T15:39:12Z")

</div>

Thanks for that!  
b.
