Ruby 通过传递output.close和input.close将2个变量更改为1
我正在艰难地学习Ruby,我一直在清理代码。当我运行ori代码时,它不会产生任何错误。当我运行带有更改的代码时,它会运行所有内容,但也会添加一条错误消息。我不太清楚为什么。请帮忙Ruby 通过传递output.close和input.close将2个变量更改为1,ruby,Ruby,我正在艰难地学习Ruby,我一直在清理代码。当我运行ori代码时,它不会产生任何错误。当我运行带有更改的代码时,它会运行所有内容,但也会添加一条错误消息。我不太清楚为什么。请帮忙 ex17.rb:19:in `<main>': undefined method `close' for #<String:0x007febe4054c18> (NoMethodError) 我所做的更改是将输入和indata放在一起 from_file, to_file = ARGV scr
ex17.rb:19:in `<main>': undefined method `close' for #<String:0x007febe4054c18> (NoMethodError)
我所做的更改是将输入和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()
在第1个代码中,在第
input=File.open(从_文件)
行中,input
的类型是File
但是在第二个代码中,在第
input=File.open(from_File).read()行中,input
的类型是String
。而且String
没有close
方法。在这种情况下,如果我取出第二个代码的close方法。两个代码的输出完全相同,这是否意味着结果相同?
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()