File#size

R

Roger Pack

I am thinking this would be a nice function to have in the stdlib.

File#size

thoughts?
-=r
 
D

Dylan Evans

[Note: parts of this message were removed to make it a legal post.]

How about file.stat.size ?
 
R

Roger Pack

Dylan said:
How about file.stat.size ?

ooh that's really close.
I might want one that takes into account unflushed buffers, though.
Apparently .stat.size doesn't quite.
=> 3

But at least it's pretty close.
-=r
 
D

Dylan Evans

[Note: parts of this message were removed to make it a legal post.]

I didn't know that, but it's something to bear in mind. I haven't read the
code but i suspect that it is a shortcut for File::Stat.new('file').size
 
T

Tim Hunter

Roger said:
It appears that #tell gives you the size of the file you're writing to,
and File#stat.size tells you the filesize of files you're reading from,
is that right?

#tell tells you where you're at in the file, not the size of the file.
If you're in the middle of reading a file, #tell tells you how far you
are from the start of file. If you seek to offset 100 in a 100,000 byte
file, #tell returns 100, not 100,000.

$ ri IO#tell
---------------------------------------------------------------- IO#tell
ios.pos => integer
ios.tell => integer
------------------------------------------------------------------------
Returns the current offset (in bytes) of ios.

f = File.new("testfile")
f.pos #=> 0
f.gets #=> "This is line one\n"
f.pos #=> 17
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,514
Latest member
AdeleGelle

Latest Threads

Top