Forum: Ruby File#size

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2009-02-07 15:27
I am thinking this would be a nice function to have in the stdlib.

File#size

thoughts?
-=r
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-07 15:58
(Received via mailing list)
On 07.02.2009 15:26, Roger Pack 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
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-07 16:11
(Received via mailing list)
Hi --

On Sat, 7 Feb 2009, Robert Klemme wrote:

> On 07.02.2009 15:26, Roger Pack 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!
42773b24e0c3fb506a8a875c058a9dd7?d=identicon&s=25 Dylan Evans (Guest)
on 2009-02-07 16:12
(Received via mailing list)
How about file.stat.size ?
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2009-02-07 16:35
Dylan Evans 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
42773b24e0c3fb506a8a875c058a9dd7?d=identicon&s=25 Dylan Evans (Guest)
on 2009-02-07 16:49
(Received via mailing list)
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-07 17:35
(Received via mailing list)
On 07.02.2009 16:34, Roger Pack 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
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2009-02-07 18:16
> 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?
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2009-02-07 21:19
(Received via mailing list)
Roger Pack 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
E67536b7234fcf4e0c01b100826cfc60?d=identicon&s=25 Lars Haugseth (Guest)
on 2009-02-11 16:45
(Received via mailing list)
* "David A. Black" <dblack@rubypal.com> wrote:
> >> 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/rdo...

--
Lars Haugseth

"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
This topic is locked and can not be replied to.