Ruby 模块方法访问 [英] Ruby Module Method Access
本文介绍了Ruby 模块方法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用于常量的 ruby 模块.它有一个变量列表和 1 个应用格式的方法.我似乎无法访问此模块中的方法.知道为什么吗?
I have a ruby module for constants. It has a list of variables and 1 method which applies formatting. I can't seem to access the method in this module. Any idea why?
推荐答案
如果你include
这个模块,这个方法就变成了一个实例方法,但是如果你extend
模块然后它成为一个类方法.
If you include
the module the method becomes an instance method but if you extend
the module then it becomes a class method.
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
这篇关于Ruby 模块方法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文