在非Rails Ruby应用程序中包含Rails时间助手

时间:2021-11-19 23:18:20

I primarily use Rails to develop in and I love the 1.hour.from_now, 34.minutes, and 24.megabytes helpers that Rails has built into it. However right now I am building just a Ruby application having these helpers would be nice. Is it possible to just get these helpers in a Ruby application without having to bring in the whole Rails framework?

我主要使用Rails进行开发,我喜欢Rails内置的1.hour.from_now,34.minutes和24.megabytes助手。但是现在我正在构建一个拥有这些帮助器的Ruby应用程序会很好。是否可以在Ruby应用程序中获取这些帮助程序而无需引入整个Rails框架?

1 个解决方案

#1


9  

These methods come from ActiveSupport's core extensions (specifically ones on Integer & Numeric), so you can just require them:

这些方法来自ActiveSupport的核心扩展(特别是Integer和Numeric上的扩展),因此您可以只需要它们:

require 'active_support/core_ext/integer/time'
require 'active_support/core_ext/numeric/time'

Or if you want all core extensions ActiveSupport provides:

或者,如果您想要ActiveSupport提供的所有核心扩展:

require 'active_support/core_ext'

Or if you want all of ActiveSupport (core extensions included):

或者,如果您想要所有ActiveSupport(包括核心扩展):

require 'active_support/all'

Of course you have to ensure the activesupport gem is installed.

当然,您必须确保已安装activesupport gem。

#1


9  

These methods come from ActiveSupport's core extensions (specifically ones on Integer & Numeric), so you can just require them:

这些方法来自ActiveSupport的核心扩展(特别是Integer和Numeric上的扩展),因此您可以只需要它们:

require 'active_support/core_ext/integer/time'
require 'active_support/core_ext/numeric/time'

Or if you want all core extensions ActiveSupport provides:

或者,如果您想要ActiveSupport提供的所有核心扩展:

require 'active_support/core_ext'

Or if you want all of ActiveSupport (core extensions included):

或者,如果您想要所有ActiveSupport(包括核心扩展):

require 'active_support/all'

Of course you have to ensure the activesupport gem is installed.

当然,您必须确保已安装activesupport gem。