avk
avk

Reputation: 93

Basic Ruby Time Operations Not Working

I've been trying to test out some stuff in irb and it doesn't recognize commands like

2.weeks.ago

and

10.hours.ago

The error I get is the following:

1.9.3p258 :002 > 2.weeks.ago
NoMethodError: undefined method `weeks' for 2:Fixnum
from (irb):2
from /Users/Avneesh/.rvm/rubies/ruby-1.9.3-head/bin/irb:16:in `<main>'

For reference... Ruby version: 1.9.3p258 OS: Mac OS X 10.8

Any ideas on why this is happening? I'm pretty sure I don't need to require/include any modules/libraries. Any help would be appreciated.

Thanks.

Upvotes: 4

Views: 1551

Answers (1)

Dogbert
Dogbert

Reputation: 222358

These methods are defined in activesupport gem. You need to include active_support/all

There are two ways:

➜  ~  irb -ractive_support/all
1.9.3-p125 :001 > 2.weeks.ago
 => 2012-07-22 14:06:10 +0530 
1.9.3-p125 :002 > exit


➜  ~  irb
1.9.3-p125 :001 > require 'active_support/all'
 => true 
1.9.3-p125 :002 > 2.weeks.ago
 => 2012-07-22 14:06:22 +0530 
1.9.3-p125 :003 > 

Edit: As pointed out by Jörg in the comments, you can just require active_support/core_ext/integer/time to include Time specific functions, including this one.

Upvotes: 9

Related Questions