106

I have Active Support 3.0.3 installed and Rails 3.0.3 with Ruby 1.8.7.

When I try to use 1.week.ago I get

NoMethodError: undefined method 'week' for 1:Fixnum
from (irb):2

The other core extensions seem to work. I tried it on a friend's computer (same install specs and legacy versions are on his) with the same results.

What gives?

All of this is in IRB.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
griotspeak
  • 13,022
  • 13
  • 43
  • 54
  • You don't say whether your code is running inside a Rails application, or in a Ruby application that you want to use some Rails extensions in. – the Tin Man Nov 21 '10 at 18:42
  • Duplicate of [Use Rails 3's ActiveSupport core extensions outside rails](http://stackoverflow.com/questions/3053119/use-rails-3s-activesupport-core-extensions-outside-rails). – Arman H Mar 30 '13 at 06:28

5 Answers5

151

Since using Rails should handle this automatically I'm going to assume you're trying to add Active Support to a non-Rails script.

Read "How to Load Core Extensions".

Active Support's methods got broken into smaller groups in Rails 3, so we don't end up loading a lot of unneeded stuff with a simple require 'activesupport'. Now we have to do things like

require 'active_support/core_ext/object/blank'

If you don't care about granularity, you can choose to load bigger chunks. If you want everything in one big gulp use...

For 1.9.2:

rvm 1.9.2
irb -f
irb(main):001:0> require 'active_support/all'
=> true
irb(main):002:0> 1.week.ago
=> 2010-11-14 17:56:16 -0700
irb(main):003:0> 

For 1.8.7:

rvm 1.8.7
irb -f
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'active_support/all'
=> true
irb(main):003:0> 1.week.ago
=> Sun Nov 14 17:54:19 -0700 2010
irb(main):004:0> 
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • i have to require rubygems and active_record first, but this fix DID work in irb after that. – griotspeak Nov 22 '10 at 00:40
  • All I did was what is in the example. I'll add what I did to run it in 1.8.7 also. – the Tin Man Nov 22 '10 at 00:54
  • 1
    if you need this all the time - let say you are playing with some code or something, you can add or modify .irbrc file to require all libs that you use all the time. It will load them automatically on start. I.E. I use awesome_print, hirb and some custom methods all the time - so I just put them there and don't have to think twice for things like 1.hour.ago or ap {:a => 3, :b => "nick"} to work. – konung Nov 24 '10 at 22:58
  • 1
    Check out [`irbtools`](https://github.com/janlelis/irbtools). It includes all sorts of nice tweaks to IRB. And, if you need to disable them temporary, call `irb -f` and it will not load them. – the Tin Man Nov 24 '10 at 23:18
  • I'll add that the new hotness is [Pry](https://github.com/pry/pry) for a Ruby interactive session. Very impressive. – the Tin Man Jun 16 '12 at 16:31
30

You can granularly add libraries via the already mentioned

require 'active_support/core_ext/some_class/some_file'

There is also another level up where you can

require 'active_support/core_ext/some_class'

But, at the moment, this is unfortunately not available for Time, Date and DateTime.

A way around this is to require 'active_support/time' which will give you Time, Date and DateTime which would solve the OP was asking for without requiring everything.


My Rails patch, which adds active_support/core_ext/date and date_time, made it into Rails v4.0.0, so now you can require these individually. YAY!

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
mraaroncruz
  • 3,780
  • 2
  • 32
  • 31
-1

In my case the following link worked:

https://bundler.io/blog/2019/01/04/an-update-on-the-bundler-2-release.html

$ cat Gemfile.lock | grep -A 1 "BUNDLED WITH"
BUNDLED WITH
   1.17.3

$ gem install bundler -v '1.17.3'
Kazuya Gosho
  • 996
  • 13
  • 14
-3

You can :
require 'active_support/core_ext'
or :
require 'active_support/all'

Lane
  • 4,682
  • 1
  • 36
  • 20
-4

Does this work from the console? This is working for me:

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.6.5
BuildVersion:   10H574

$ rails c
Loading development environment (Rails 3.0.3)
>> 1.week.ago
=> Sun, 14 Nov 2010 16:57:18 UTC +00:00
Paul Schreiber
  • 12,531
  • 4
  • 41
  • 63