如何使用礼物? 在Ruby项目中?

我写了一个脚本,它使用Rails对Ruby核心Object类的扩展,现在我想把它自己分开。 而不是重写这些,我可以使用任何库或扩展来使它们独立可用吗?

您不必运行Rails即可使用

require 'active_support/all' 

或者如果你只想要一个特定的扩展名,比如blank? 对于字符串然后只是

 require 'active_support/core_ext/string' 

我抬头看了 present? 它实际上被定义为

 def present? !blank? end 

在活动支持中定义为空白

 def blank? respond_to?(:empty?) ? !!empty? : !self end 

您可以从ActiveSupport中获取特定function。 在这种情况下:

 require 'active_support/core_ext/object/blank' [].present? #=> false