如何使用rails之外的active_support提供的方法

iro*_*and 3 ruby ruby-on-rails

如何使用ActiveSupport模块中定义的方法?

例如,我想使用camelize方法.

我尝试了这段代码,但它没有用于undefined method错误.

require 'active_support'
"foo_bar".camelize
Run Code Online (Sandbox Code Playgroud)

Rya*_*igg 5

您可以只需要提供camelize的部分,而不是所有的Active Support:

require 'active_support/core_ext/string'
"camel_this".camelize => "CamelThis"
Run Code Online (Sandbox Code Playgroud)

  • 或者甚至只是“需要'active_support/core_ext/string/inflections'”。 (2认同)