4

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.

avk
  • 93
  • 1
  • 5

1 Answers1

9

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.

Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • Thanks, that worked. Is there a way to do it without explicitly calling require 'active_support/all'? I feel like I've done it before... – avk Aug 05 '12 at 08:41
  • 1
    Only in irb? You can add that require statement in your `~/.irbrc` – Dogbert Aug 05 '12 at 08:42
  • 1
    You don't need all of Active Support just to use those extensions. `active_support/core_ext/integer/time` is enough. – Jörg W Mittag Aug 05 '12 at 11:19