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.
Roger P. (Guest)
on 2009-02-07 16:27
I am thinking this would be a nice function to have in the stdlib.

File#size

thoughts?
-=r
Robert K. (Guest)
on 2009-02-07 16:58
(Received via mailing list)
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
David A. Black (Guest)
on 2009-02-07 17:11
(Received via mailing list)
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!
Dylan E. (Guest)
on 2009-02-07 17:12
(Received via mailing list)
How about file.stat.size ?
Roger P. (Guest)
on 2009-02-07 17:35
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
Dylan E. (Guest)
on 2009-02-07 17: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
Robert K. (Guest)
on 2009-02-07 18:35
(Received via mailing list)
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
Roger P. (Guest)
on 2009-02-07 19: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?
Tim H. (Guest)
on 2009-02-07 22:19
(Received via mailing list)
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
Lars H. (Guest)
on 2009-02-11 17:45
(Received via mailing list)
* "David A. Black" <removed_email_address@domain.invalid> 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 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
This topic is locked and can not be replied to.