# Time expiration code

**URL:** <https://www.ruby-forum.com/t/time-expiration-code/112513>\
**Category:** Rails\
**Created:** [August 20, 2007, 2:32pm UTC](https://www.ruby-forum.com/t/time-expiration-code/112513 "2007-08-20T14:32:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [August 20, 2007, 2:32pm UTC](https://www.ruby-forum.com/t/time-expiration-code/112513/1 "2007-08-20T14:32:02Z")

</div>

I want to create a simple one-line function (to be used by a  
before\_filter) that expires the session[:updated\_at] data after twenty  
minutes.

To be honest, if I knew Ruby a little more I wouldn’t be asking this  
question, but exactly how does the addition and the subtraction of  
Time work in Rails? Are they treated as time variables, or integer  
variables of some form?

## Would something like this work?

private  
def expire\_session  
session[:updated\_at]=nil if Time.now-session[:updated\_at]\<20.minutes  
end

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [August 20, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/time-expiration-code/112513/2 "2007-08-20T14:45:05Z")

</div>

Hi –

On Mon, 20 Aug 2007, Taro wrote:

> ## Would something like this work?
> 
> private  
> def expire\_session  
> session[:updated\_at]=nil if Time.now-session[:updated\_at]\<20.minutes

I think you want \> rather than \<.

> end

You can always try these things in the application console (not  
regular irb, because a lot of stuff like “minutes” requires  
ActiveSupport [which you can load in irb, but it’s easier to use the  
Rails console]):

> > update = Time.now; until Time.now - update \> 5.seconds;  
> > ?\> puts “waiting…”; sleep 1; end; puts “done”  
> > waiting…  
> > waiting…  
> > waiting…  
> > waiting…  
> > waiting…  
> > done

I think there are plugins that do more fine-grained and featureful  
observing for you, but anyway, that’s the basic idea.

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [August 20, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/time-expiration-code/112513/3 "2007-08-20T14:51:06Z")

</div>

> I think you want \> rather than \<.
> 
> > end

Whoops, yes you’re right about that. Regardless, if Time.now-  
session[:updated\_at] works, than I really have nothing to worry about.
