Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Active Support core extensions

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.

like image 650
griotspeak Avatar asked Nov 21 '10 16:11

griotspeak


People also ask

What is ActiveSupport in Rails?

Active Support is the Ruby on Rails component responsible for providing Ruby language extensions and utilities. It offers a richer bottom-line at the language level, targeted both at the development of Rails applications, and at the development of Ruby on Rails itself.

What is ActiveSupport concern?

ActiveSupport's Concern module allows us to mix in callbacks, class and instance methods, and create associations on target objects. This module has an included method, which takes a block, as well as an append_features method and class_methods block, which you can read about in the source code.

How to load core extensions to loaderror extensions?

Extensions to LoadError Extensions to Pathname existence 1 How to Load Core Extensions 1.1 Stand-Alone Active Support In order to have the smallest default footprint possible, Active Support loads the minimum dependencies by default. It is broken in small pieces so that only the desired extensions can be loaded.

What is stand-alone active support?

1.1 Stand-Alone Active Support In order to have the smallest default footprint possible, Active Support loads the minimum dependencies by default. It is broken in small pieces so that only the desired extensions can be loaded. It also has some convenience entry points to load related extensions in one shot, even everything.

What is active support in Ruby on rails?

1.2 Active Support Within a Ruby on Rails Application A Ruby on Rails application loads all Active Support unless config.active_support.bareis true. In that case, the application will only load what the framework itself cherry-picks for its own needs, and can still cherry-pick itself at any granularity level, as explained in the previous section.

How does active support define datetime?

Active Support defines DateTime.currentto be like Time.now.to_datetime, except that it honors the user time zone, if defined. The instance predicates past?and future?are defined relative to DateTime.current. Defined in active_support/core_ext/date_time/calculations.rb. 16.1.2 Other Extensions 16.1.2.1 seconds_since_midnight


1 Answers

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>  
like image 127
the Tin Man Avatar answered Oct 03 '22 17:10

the Tin Man