如果看不见你的助手代码,则很难说出,但include
将把该模块中的所有方法插入instances。 <代码>extend用于将方法带入 >>>>>。 因此,如果你刚刚掌握了编号Helper的方法,这些方法就会被引入到MyClas的所有情况,而不是一类。
The way that lots of Rails extensions work is using techniques that have been consolidated into ActiveSupport::Concern. Here is a good overview.
基本上,扩大积极的支持:在你的模块中,你可以确定,在称为“级治疗和症状”的分模块中,你想要在包括你的模块的课堂和例中增加什么功能。 例如:
module Foo
extend ActiveSupport::Concern
module ClassMethods
def bar
puts "I m a Bar!"
end
end
module InstanceMethods
def baz
puts "I m a Baz!"
end
end
end
class Quox
include Foo
end
Quox.bar
=> "I m a Bar"
Quox.new.baz
=> "I m a Baz"
I ve used this before to do things like define the bar function in ClassMethods, then also make it available to instances by defining a bar method of the same name that just calls this.class.bar, making it callable from both. There are lots of other helpful things that ActiveSupport::Concern does, like allowing you to define blocks that are called back when the module is included.
Now, this is happening here specifically because you re include
ing your helper, which might indicate that this functionality is more general-purpose than a helper - helpers are only automatically included in views, since they are only intended to help with views. If you want to use your helper in a view, you could use the helper_method
macro in your class to make that method visible to your views, or, even better, make a module as above and not think about it as a helper, but use include to mix it in to the classes you want to use it in. I think I would go that route - there s nothing that says you can t make a HumanReadableNumber module and include
it in NumberHelper to make it easily available across your views.