我试图(Ruby v2.5.3)
在 Windows 10 上执行一个 Ruby 程序,我基本上打开一个文件(sample1.txt, sample2.txt)
并将其内容(3 个名称的列表)附加到一个空文件(result.txt)
,然后对文件的内容进行排序result.txt
(下面的整个代码)。但是,当我在 RubyMine 2019.1 上运行此代码时,我收到错误消息
Traceback (most recent call last):
6: from C:/Users/tanveer.dey/RubymineProjects/src/Assignment9.rb:5:in `<main>'
5: from C:/Users/tanveer.dey/RubymineProjects/src/Assignment9.rb:5:in `each'
4: from C:/Users/tanveer.dey/RubymineProjects/src/Assignment9.rb:7:in `block in <main>'
3: from C:/Users/tanveer.dey/RubymineProjects/src/Assignment9.rb:7:in `each'
2: from C:/Users/tanveer.dey/RubymineProjects/src/Assignment9.rb:8:in `block (2 levels) in <main>'
1: from C:/Users/tanveer.dey/RubymineProjects/src/Assignment9.rb:8:in `open'
C:/Users/tanveer.dey/RubymineProjects/src/Assignment9.rb:8:in `initialize': Permission denied @ rb_sysopen - result.txt (Errno::EACCES)
所有文件都具有完全权限。但是,当我通过 IRB 终端执行相同的代码时,它工作得非常好。
我刚刚开始研究 Ruby,但在任何地方都找不到解决此问题的方法。非常感谢您的帮助。
file_array = ["sample1.txt", "sample2.txt"]
file_array.each do |x|
file = File.open(x,'r')
file.each do |h|
open('result.txt', 'a') do |f|
puts f << h
end
end
open('result.txt','a') do |f|
puts f << "\n"
end
end
new_array = File.readlines('result.txt').sort
File.open('result.txt','w') do |file|
new_array.each {|n| file.puts(n)}
end