3
鑑於情況下,例如:如何將類級方法擴展到Rails中的單獨模塊?
module Extension
def self.included(recipient)
recipient.extend(ModelClassMethods)
end
module ModelClassMethods
def self.msg
puts 'Hi from module'
end
end
end
class B
include Extension
end
爲什麼B.msg不可用?
>> B.msg
NoMethodError: undefined method `msg' for B:Class
from (irb):16
我在想這個錯誤的方式嗎?似乎這不應該是很難完成的。
約翰:你可能要編輯您的例子,以便它包含完整的代碼示例和你的變化(變化粗體) – 2010-02-10 03:58:15
好主意 - 做。 – 2010-02-10 10:34:18
一旦我看到您將這些方法聲明爲實例方法,就立即點擊。謝謝! – Matthew 2010-02-13 01:09:10