我需要active_support/inflector,现在我可以在字符串上使用pluralize方法了。
require 'active_support/inflector'
module Foodie
class Food
def self.pluralize(word)
word.pluralize
end
end
end
p Foodie::Food.pluralize("foo") #=> foos但我原以为必须将ActiveSupport::Inflector模块作为一个混合器才能工作。
为什么我不需要这样的东西
module Foodie
class Food
include ActiveSupport::Inflector
def self.pluralize(word)
word.pluralize
end
end
end
p Foodie::Food.pluralize("foo") #=> foos同样,上面的例子仍然有效。
我认为,需要一个文件的全部目的就是让您能够访问该文件中的类/模块/方法/变量,但它不一定在所有模块和类中都包含该功能。
可能是因为带有Inflector模块的ruby文件正在打开string类吗?如本例所示:
foo.rb:
module Foo
end
class String
def cats
self + " cats"
end
endbar.rb:
require_relative 'foo'
p "foobar".cats #=> "foobar cats"希望得到一些指导:)
发布于 2016-11-30 18:51:40
https://stackoverflow.com/questions/40895911
复制相似问题