Reputation: 43204
The documentation say:
File.size(file_name) => integer
Returns the size of file_name.File.size?(file_name) => Integer or nil Returns nil if file_name doesn‘t exist or has zero size, the size of the file otherwise.
On practice (ruby 1.8.7 i386-mswin32):
File.size?('c:/dir')
# => nill
File.size('c:/dir')
# => 0
The nil makes sence for me, but 0? I would expect an exception instead. Do anybody see reason for this?
Upvotes: 0
Views: 3875
Reputation: 797
Exceptions are as a general rule slow, so whenever the issue is not critical, it's better to use return flags for efficiency reasons. As long as the file/directory exists, I see no use for an exception, and in fact would find one annoying. Like this, calculating total file size is simply adding them up with no error-handling required.
Upvotes: 1
Reputation: 223123
Directories are files. Well, I suppose in some operating systems they aren't, but in all Unix-based ones they are.
Of course, in Unix systems, directories in "regular" file systems (i.e., ones that have real files, not /proc
or the like) have non-zero size too.
File.size('/etc')
=> 12288
Upvotes: 2