module Test
def self.model_method
puts "this is a module method"
end
end
class A
include Test
end
A.model_method
này sẽ lỗi với:Tại sao phương pháp 'tự' của mô-đun không thể trở thành một phương thức đơn lớp?
phương pháp xác định 'model_method' cho A: Class (NoMethodError)
Nhưng khi tôi sử dụng metaclass của A. hoạt động:
module Test
def model_method
puts "this is a module method"
end
end
class A
class << self
include Test
end
end
A.model_method
Ai đó có thể giải thích điều này?
thể trùng lặp của [? Là có thể để xác định một phương pháp lớp học trong một module] (http://stackoverflow.com/questions/4699355/is-that-possible-to-define -a-class-method-in-a-module) –