File#size


#1

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

File#size

thoughts?
-=r


#2

On 07.02.2009 15:26, Roger P. wrote:

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

File#size

thoughts?

Others seems to agree

http://www.ruby-doc.org/core/classes/File.html#M002590

robert


#3

Hi –

On Sat, 7 Feb 2009, Robert K. wrote:

On 07.02.2009 15:26, Roger P. wrote:

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

File#size

thoughts?

Others seems to agree

http://www.ruby-doc.org/core/classes/File.html#M002590

I think Roger means an instance method.

David


David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!


#4

How about file.stat.size ?


#5

Dylan E. wrote:

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.

a.write ‘abc’
=> 3

a.stat.size
=> 0

a.flush
=> #<File:README>

a.stat.size
=> 3

But at least it’s pretty close.
-=r


#6

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


#7

On 07.02.2009 16:34, Roger P. wrote:

=> 0

a.flush
=> #<File:README>

a.stat.size
=> 3

But at least it’s pretty close.
-=r

So you want to know how much you have written? Then use #tell
http://www.ruby-doc.org/core/classes/IO.html#M002304

robert


#8

So you want to know how much you have written? Then use #tell
http://www.ruby-doc.org/core/classes/IO.html#M002304

robert

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?


#9

Roger P. wrote:

So you want to know how much you have written? Then use #tell
http://www.ruby-doc.org/core/classes/IO.html#M002304

robert

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

#10

thoughts?

Others seems to agree

http://www.ruby-doc.org/core/classes/File.html#M002590

I think Roger means an instance method.

There are many other File class methods for which it would be convenient
to have instance method counterparts. This and much more can be found in
the standard library Pathname:

http://www.ruby-doc.org/stdlib/libdoc/pathname/rdoc/classes/Pathname.html


Lars H.

“If anyone disagrees with anything I say, I am quite prepared not only
to
retract it, but also to deny under oath that I ever said it.” -Tom
Lehrer