아마도 구조체에 대해 충분히 알지 못하고 맹목적으로 사용하고 있었지만 아래 결과는 나에게 비합리적으로 보입니다.구조체에서이 변수가 사라지는 이유는 무엇입니까?
class VarTest < Struct.new(:email)
def perform
puts "Start: #{email}"
if email == "nothing"
email = "bad email"
end
puts "End: #{email}"
end
end
VarTest.new("[email protected]").perform
예기치 않은 출력 :
Start: [email protected]
End:
나는에 코드를 변경하는 경우 :
class VarTest < Struct.new(:email)
def perform
e = email
puts "Start: #{e}"
if e == "nothing"
e = "bad email"
end
puts "End: #{e}"
end
end
VarTest.new("[email protected]").perform
우리는 예상 출력을 얻을 :
Start: [email protected]
End: [email protected]
누군가가이 일을 설명해 주시겠습니까?
감사합니다.
실제 목표는 무엇입니까? 즉, 왜 VarTest = Struct.new (: email) 대신'VarTest.new ('[email protected]') 대신 상속을 사용하고 있습니까? –
지연된 작업이 대기열에 포함되어 사용 중입니다. 그럼에도 불구하고이 방법으로이 동작을 사용하는 이유는 무엇입니까? – chrishomer