我很迷茫。我知道如何使用調用者來獲取調用者方法,但是你怎樣使用調用者類呢?Ruby繼承獲取來電者類名
例如:
class Testing
def return_caller_class
return caller_class
end
end
class Parent
attr_accessor :test_me
def initialize
self.test_me = Testing.new
end
end
class Child < Parent
end
class GrandChild < Child
end
test_Parent = Parent.new
test_Child = Child.new
test_GrandChild = GrandChild.new
puts test_Parent.test_me.return_caller_class => Parent
puts test_Child.test_me.return_caller_class => Child
puts test_GrandChild.test_me.return_caller_class => GrandChild
謝謝!
編輯:
我試着做以下
class Testing
def return_caller_class
return caller[0][/`.*'/][1..-2]
end
end
,輸出是:
{"
"=>Parent}
{"
"=>Child}
{"
"=>GrandChild}
要解釋一下我的問題更好。
我會輸出到顯示這不是
Parent
Child
GrandChild
看看https://www.ruby-forum.com/topic/161460 – bjhaid
謝謝,我現在檢查一下。 – user3163916
雖然這個問題沒有解決。 OP決定結束這個問題。 – user3163916