def singleton_class
class << self
self
end
end
class Human
proc = lambda { puts 'proc says my class is ' + self.name.to_s }
singleton_class.instance_eval do
define_method(:lab) do
proc.call
end
end
end
class Developer < Human
end
Human.lab # class is Human
Developer.lab # class is Human ; oops
以下解決方案的工作。Instance_eval:爲什麼子類的類是超類
def singleton_class
class << self
self
end
end
class Human
proc = lambda { puts 'proc says my class is ' + self.name.to_s }
singleton_class.instance_eval do
define_method(:lab) do
self.instance_eval &proc
end
end
end
class Developer < Human
end
Human.lab # class is Human
Developer.lab # class is Human ; oops
爲什麼Developer.lab報告它是Human?當調用Developer.lab時,可以做些什麼以便proc報告開發人員。
它的工作。不知道爲什麼。謝謝。 – 2010-05-03 14:46:44
希望我的回答能夠揭示_why_ – 2010-05-03 16:12:19