Ruby模块方法访问

我有一个常量的ruby模块。 它有一个变量列表和一个应用格式的方法。 我似乎无法访问此模块中的方法。 知道为什么吗?

如果include模块,则该方法将成为实例方法,但如果extend模块,则它将成为类方法

 module Const def format puts 'Done!' end end class Car include Const end Car.new.format # Done! Car.format # NoMethodError: undefined method format for Car:Class class Bus extend Const end Bus.format # Done! Bus.new.format # NoMethodError: undefined method format 
 module Foo def self.hello # This is a class method puts "self.hello" end def hello # When you include this module, it becomes an instance method puts "hello" end end Foo.hello #=> self.hello class Bar include Foo end Bar.new.hello #=> hello 

通常,对于模块,这些事情应该发生:

– > application.rb中的自动加载路径,添加行:

 config.autoload_paths += %W(#{config.root}/lib) 

– >将模块放在/ lib中

– >包含’include NAMEOFMODULE’的模块

(如果模块有像game_engine这样的下划线,你需要’包含GameEngine’)