我正在编写一个涉及日期比较的 Ruby 脚本。
为了保持可读性,我想做像
1.day.ago
这样的事情。我认为这就像将 gem 'activesupport'
添加到我的 gemfile 并需要 require 'active_support'
一样简单。但这不起作用。
我走得更远:
require "active_support/core_ext/date/calculations"
require "active_support/core_ext/integer/time"
require "active_support/core_ext/time"
但我还不在那里:
1.day.ago
#NameError: uninitialized constant ActiveSupport::IsolatedExecutionState
#
# ::ActiveSupport::IsolatedExecutionState[:time_zone] || zone_default
…我不确定我还需要什么。我如何在我的 vanilla ruby 脚本中使用所有活动记录的日期/时间方法?
你只需要包括
ActiveSupport::IsolatedExecutionState
例如
# 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
为什么在“active_support/core_ext/time/zones”中不需要这个,我不能说,因为我觉得应该避免这个问题。
如果你真的想包括所有
activesupport
你非常接近但是实际的要求声明是require 'active_support/all'
这个文件的内容很简单:
require "active_support"
require "active_support/time"
require "active_support/core_ext"