I'm writing a Ruby script that involves date comparisons.
To keep it readable, I'd like to do things like 1.day.ago
. I thought it would be as easy as adding gem 'activesupport'
to my gemfile and requiring require 'active_support'
. But that doesn't work.
I've gone further:
require "active_support/core_ext/date/calculations"
require "active_support/core_ext/integer/time"
require "active_support/core_ext/time"
But I'm not quite there:
1.day.ago
#NameError: uninitialized constant ActiveSupport::IsolatedExecutionState
#
# ::ActiveSupport::IsolatedExecutionState[:time_zone] || zone_default
…I'm not sure what else I need to require. How do I use all of active record's date/time methods in my vanilla ruby script?
You just need to include ActiveSupport::IsolatedExecutionState
e.g.
# This is not needed because active_support/core_ext/integer/time already requires it
# require "active_support/core_ext/date/calculations"
# This is not really needed either as almost all of it will
# be required by the requirements in active_support/core_ext/integer/time
# require "active_support/core_ext/time"
require "active_support/core_ext/integer/time"
require "active_support/isolated_execution_state.rb"
1.day.ago
#=> 2023-02-20 19:18:49.686473704 +0000
Why this is not required in "active_support/core_ext/time/zones", I can't say as I feel like it should be to avoid this issue.
If you really want to include all of activesupport
you were very close however the actual require statement is require 'active_support/all'
The contents of this file are simply:
require "active_support"
require "active_support/time"
require "active_support/core_ext"