Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 638
Andrew Avatar asked Nov 30 '11 16:11

Andrew


1 Answers

This is in activesupport

include like this

require 'rubygems'
require 'active_support/core_ext/date/conversions'
like image 123
Joseph Le Brech Avatar answered Nov 15 '22 21:11

Joseph Le Brech