0
我正在通過Learn Ruby The Hard Way和我被困在清理代碼的過程中。當我運行ori代碼時,它不會產生任何錯誤。當我使用更改運行代碼時,它會運行所有內容,但也會添加錯誤消息。我不太清楚爲什麼。請幫忙。通過傳遞output.close和input.close將2個變量更改爲1
ex17.rb:19:in `<main>': undefined method `close' for #<String:0x007febe4054c18> (NoMethodError)
ori。
from_file, to_file = ARGV
script = $0
puts "Copying from #{from_file} to #{to_file}"
#we could do these two on one line too, how?
input = File.open(from_file)
indata = input.read()
puts "The input file is #{indata.length} bytes long"
puts "Does the output file exist? #{File.exist? to_file}"
output = File.open(to_file, "w")
output.write(indata)
puts "Alright, all done."
output.close()
input.close()
我所做的更改是將輸入和indata放在一起。
from_file, to_file = ARGV
script = $0
puts "Copying from #{from_file} to #{to_file}"
#we could do these two on one line too, how?
input = File.open(from_file).read()
puts "The input file is #{input.length} bytes long"
puts "Does the output file exist? #{File.exist? to_file}"
output = File.open(to_file, "w")
output.write(input)
puts "Alright, all done."
output.close()
input.close()
在這種情況下,如果我拿出第二個代碼的close方法。兩個代碼的輸出完全相同,這是否意味着它是相同的結果? – jmoon90