내가이 일을하고있어 작동 :Proc.new가 아니라 to_proc을 사용하여 instance_eval을 호출하는 중 오류가 발생하는 이유는 무엇입니까?
class B
def value
"X"
end
end
class A
def initialize(context)
@context = context
end
def m
Proc.new do
value
end
end
def execute
@context.instance_eval(&m)
end
end
A.new(B.new).execute #=> "X"
그러나 m.to_proc
를 호출
class B
def value
"X"
end
end
class A
def initialize(context)
@context = context
end
def m
value
end
def execute
@context.instance_eval(&m.to_proc)
end
end
A.new(B.new).execute #=> NameError: undefined local variable or method `value' for #<A:0x007fae2ab02040 @context=#<B:0x007fae2ab02108>>
나는이 두 가지 예는 다른 이유를 알고 싶어 ... 작동하지 않는 방법을 만드는 방법
to_proc
미안 해요, 블록의 PROC로 변환하는 "&"전에'm.to_proc'를 놓친 여전히 작동하지 않습니다. – Leantraxxx