Andrew
Andrew

Reputation: 238667

How to use Rails date helper methods outside of Rails?

I am converting some Rails controller code to be generic Ruby code. I came across this:

expiration_date = 1.hours.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')

...and realized this is not standard Ruby. I get this error message:

undefined method `hours' for 1:Fixnum (NoMethodError)

How can I convert this to standard Ruby, or require/include the necessary libraries to make this work?

Solution:

Require the ActiveSupport Numeric class extensions:

require 'rubygems'
require 'active_support/core_ext/numeric/time'

Upvotes: 9

Views: 3751

Answers (1)

Joseph Le Brech
Joseph Le Brech

Reputation: 6653

This is in activesupport

include like this

require 'rubygems'
require 'active_support/core_ext/date/conversions'

Upvotes: 7

Related Questions