Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 通过传递output.close和input.close将2个变量更改为1_Ruby - Fatal编程技术网

Ruby 通过传递output.close和input.close将2个变量更改为1

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

我正在艰难地学习Ruby,我一直在清理代码。当我运行ori代码时,它不会产生任何错误。当我运行带有更改的代码时,它会运行所有内容,但也会添加一条错误消息。我不太清楚为什么。请帮忙

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()