데모 코드를 보면 :인스턴스에서 클래스 변수에 어떻게 액세스합니까?
class A
def method_a
puts "this is method_a"
end
end
class B < A
def self.hide_method name
if instance_methods.include? name
@hidden_mthod ||= {}
@hidden_mthod[name] = instance_method name
undef_method name
end
end
def self.invoke_hidden_methods
puts @hidden_mthod.inspect
end
def bound_method name
self.class.class_variable_get(:@hidden_mthod)[name].bind(self)
end
end
b = B.new
b.method_a
B.hide_method :method_a
B.invoke_hidden_methods
b.bound_method :method_a **#error**
b.method_a **#error**
내가 원하는 것은 내가 인스턴스 메소드와 클래스에 정의 @hidden_method 액세스 할 수있는 방법을 instance.but에 특별한 방법을 바인딩입니까?
업데이트 : 감사합니다. Boris Strandjev, Ur 정말 좋은 사람입니다. 당신이 위에서 언급 한 바와 같이, 나는 코드가 더 같이 단순화해야한다고 생각 :
def bound_method name
method_body = self.class.instance_variable_get(:@hidden_method)[name]
self.class.send :define_method, name, method_body
end
Boris Strandjev의 대답. 마지막 문장을 실행할 수 없습니다. – LeoShi