多分私は構造体について十分に分かっておらず、盲目的に使っていますが、以下の結果は私には不合理なようです。なぜこの変数は構造体で消えますか?
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:
私は、コードを変更する場合:
01: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
私たちは、予想される出力を得ます
誰かがこれで起こっていることを説明できますか?
ありがとうございました。
実際の目標は何ですか?言い換えれば、なぜあなたは 'VarTest = Struct.new(:email)'と 'VarTest.new( '[email protected]')の代わりに継承を使っていますか? –
エンキューの一部として遅延ジョブによって使用されています。それにもかかわらず、なぜこのように使用するとこの動作が起こりますか? – chrishomer