I have a text file for example. What is the best way to check in Ruby that a file is empty? File.size('test.rb') == 0
looks ugly.
我有一个文本文件例如。检查Ruby文件为空的最佳方法是什么? File.size('test.rb')== 0看起来很难看。
4 个解决方案
#2
2
The best way may be to not check at all but iterate over each line and do stuff for each iteration. 0 lines = 0 iterations = no extra code needed.
最好的方法可能是根本不检查,而是迭代每一行并为每次迭代做一些事情。 0行= 0次迭代=无需额外代码。
#3
2
File.size?('test.rb')
evaluates to nil
if the file is empty or
it does not exist. File.zero?('test.rb')
will return false is the file is empty, but it will also return false if the file is not found. Depending on your particular needs you should be careful to use the correct method.
如果文件为空或文件不存在,File.size?('test.rb')的计算结果为nil。 File.zero?('test.rb')将返回false,如果文件为空,但如果找不到该文件,它也将返回false。根据您的特定需求,您应该小心使用正确的方法。
As an example in the topic creator's question they specifically asked, "What is the best way to check in Ruby that a file is empty?" The accepted answer does this correctly and will raise a No such file or directory
error message if the file does not exist.
作为主题创建者问题的一个例子,他们特别问:“检查Ruby文件是空的最佳方法是什么?”接受的答案正确执行此操作,如果该文件不存在,将引发无此类文件或目录错误消息。
In some situations we may consider the lack of a file to be "equivalent" to an empty file.
在某些情况下,我们可能会认为缺少文件与空文件“等效”。
#4
0
As of Ruby 2.4.0, there is File.empty?.
从Ruby 2.4.0开始,有File.empty?。
(Note that it always returns false
if you pass it a directory, whether that directory is empty or not: File.empty?('/') # => false
. So use Dir.empty? for that instead, or Pathname#empty? which works for both files and directories.)
(请注意,如果将目录传递给目录,它总是返回false,无论该目录是否为空:File.empty?('/')#=> false。所以使用Dir.empty?代替,或者路径名#empty ?适用于文件和目录。)
#1
#2
2
The best way may be to not check at all but iterate over each line and do stuff for each iteration. 0 lines = 0 iterations = no extra code needed.
最好的方法可能是根本不检查,而是迭代每一行并为每次迭代做一些事情。 0行= 0次迭代=无需额外代码。
#3
2
File.size?('test.rb')
evaluates to nil
if the file is empty or
it does not exist. File.zero?('test.rb')
will return false is the file is empty, but it will also return false if the file is not found. Depending on your particular needs you should be careful to use the correct method.
如果文件为空或文件不存在,File.size?('test.rb')的计算结果为nil。 File.zero?('test.rb')将返回false,如果文件为空,但如果找不到该文件,它也将返回false。根据您的特定需求,您应该小心使用正确的方法。
As an example in the topic creator's question they specifically asked, "What is the best way to check in Ruby that a file is empty?" The accepted answer does this correctly and will raise a No such file or directory
error message if the file does not exist.
作为主题创建者问题的一个例子,他们特别问:“检查Ruby文件是空的最佳方法是什么?”接受的答案正确执行此操作,如果该文件不存在,将引发无此类文件或目录错误消息。
In some situations we may consider the lack of a file to be "equivalent" to an empty file.
在某些情况下,我们可能会认为缺少文件与空文件“等效”。
#4
0
As of Ruby 2.4.0, there is File.empty?.
从Ruby 2.4.0开始,有File.empty?。
(Note that it always returns false
if you pass it a directory, whether that directory is empty or not: File.empty?('/') # => false
. So use Dir.empty? for that instead, or Pathname#empty? which works for both files and directories.)
(请注意,如果将目录传递给目录,它总是返回false,无论该目录是否为空:File.empty?('/')#=> false。所以使用Dir.empty?代替,或者路径名#empty ?适用于文件和目录。)