Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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文件名到字符串的路径错误_Ruby_Filepath - Fatal编程技术网

ruby文件名到字符串的路径错误

ruby文件名到字符串的路径错误,ruby,filepath,Ruby,Filepath,这是我的代码: file = File.open('result.txt', 'w+').read path = Dir[ENV['HOME'] + '/Desktop/Test/*.txt'] file.puts "this is a #{path} test: " 它出现了一个错误: C:/Users/User/RubymineProjects/Comparison/test.rb:5:in `<top (required)>': private method `puts'

这是我的代码:

file = File.open('result.txt', 'w+').read

path = Dir[ENV['HOME'] + '/Desktop/Test/*.txt']

file.puts "this is a #{path} test: "
它出现了一个错误:

C:/Users/User/RubymineProjects/Comparison/test.rb:5:in `<top (required)>': private method `puts' called for "":String (NoMethodError)
    from -e:1:in `load'
    from -e:1:in `<main>'
我试过这个:

puts "this is a #{path[0]} test: "

它实现了我想要的功能,但一旦我执行
file.put
,它又会出现相同的错误。

当您执行
file.put
时,您将向现在存储在变量
file
中的字符串对象发送一个方法
#put
。这是因为
File#read
方法返回一个字符串。因此,在第一行,
file
获取
result.txt
的内容,然后将其存储在变量
文件中。然后调用
放在该字符串上。而且
String#puts`是一个私有方法,因此不能像上面代码中那样使用它

如果您打算编写结果
这是一个C:/Users/User/Desktop/Test/new_1.txt Test:
,则需要使用
文件。按以下方式打开
方法:

File.open('result.txt', 'w+') do |file|
  path = Dir[ENV['HOME'] + '/Desktop/Test/*.txt']
  file.puts "this is a #{path} test: "
  # whatever else needs to be written goes here
end
或者,如果您更喜欢命令式样式而不是块样式:

file = File.new('result.txt', 'w+')
# If you prefer `open`, that works too!
# file = File.open('result.txt', 'w+')
path = Dir[ENV['HOME'] + '/Desktop/Test/*.txt']
file.puts "this is a #{path} test: "

# ensure this is closed, or you'll have memory issues if you do this often
file.close 

在这里执行
file.put
操作时,您将向一个字符串对象发送一个方法
#put
,该对象现在存储在变量
文件中。这是因为
File#read
方法返回一个字符串。因此,在第一行,
file
获取
result.txt
的内容,然后将其存储在变量
文件中。然后调用
放在该字符串上。而且
String#puts`是一个私有方法,因此不能像上面代码中那样使用它

如果您打算编写结果
这是一个C:/Users/User/Desktop/Test/new_1.txt Test:
,则需要使用
文件。按以下方式打开
方法:

File.open('result.txt', 'w+') do |file|
  path = Dir[ENV['HOME'] + '/Desktop/Test/*.txt']
  file.puts "this is a #{path} test: "
  # whatever else needs to be written goes here
end
或者,如果您更喜欢命令式样式而不是块样式:

file = File.new('result.txt', 'w+')
# If you prefer `open`, that works too!
# file = File.open('result.txt', 'w+')
path = Dir[ENV['HOME'] + '/Desktop/Test/*.txt']
file.puts "this is a #{path} test: "

# ensure this is closed, or you'll have memory issues if you do this often
file.close 

你是想附加到文件还是打印到屏幕上?我想把结果放在文件里你是想附加到文件还是打印到屏幕上?我想把结果放在文件里谢谢!它现在打印到结果文件中,但显示为
这是一个[“C:/Users/User/Desktop/Test/new_1.txt”]测试:
如何丢失方括号和引号?当您提到
Test/*.txt
时,它意味着它是一个数组,因此此输出是正确的。但是,如果你想加入数组元素,你可以这样做:
“这是一个#{path.join(',')}测试:
在第三行,我不想加入它们,我只想输出看起来像
这是一个C:/Users/User/Desktop/test/new_1.txt测试:
,名称根据它是什么文件名改变!它现在打印到结果文件中,但显示为
这是一个[“C:/Users/User/Desktop/Test/new_1.txt”]测试:
如何丢失方括号和引号?当您提到
Test/*.txt
时,它意味着它是一个数组,因此此输出是正确的。但是,如果你想加入数组元素,你可以这样做:
“这是一个#{path.join(',')}测试:”
在第三行,我不想加入它们,我只想输出看起来像
这是一个C:/Users/User/Desktop/test/new_1.txt测试:
,名称根据文件名的不同而变化