File.read() v Pathname.read() gotcha

This is damned annoying. And does not appear to be documented, at least, obviously.

Given a file:
    111222333444555

Then File.read and IO.read work like this:

    f = File.new("filename")
    f.read(3) # -> "111"
    f.read(3) # -> "222"
    f.read(3) # -> "333"

But look what happens when I use Pathname instead of File:

    p = Pathname.new("filename")
    p.read(3) # -> "111"
    p.read(3) # -> "111"
    p.read(3) # -> "111"

But according to the docs for Pathname.read(), it's just a proxy for File.read()...

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

This is damned annoying. And does not appear to be documented, at least, obviously.

I find that pretty obvious (see below).

Given a file:
    111222333444555

Then File.read and IO.read work like this:

    f = File.new("filename")
    f.read(3) # -> "111"
    f.read(3) # -> "222"
    f.read(3) # -> "333"

But look what happens when I use Pathname instead of File:

    p = Pathname.new("filename")
    p.read(3) # -> "111"
    p.read(3) # -> "111"
    p.read(3) # -> "111"

But according to the docs for Pathname.read(), it's just a proxy for File.read()...

It is, but you need to be aware of the purpose of these classes:
instances of File represent a file handle, a connection to a specific
file system object that can be used for reading or writing (or both).
Consequently it will have an idea of a current read or write position.
Instances of Pathname on the other hand represent a file _name_ and
thus cannot have an idea of a current position.

You can find this in the documentation, right at the beginning:

"A File is an abstraction of any file object accessible by the program
and is closely associated with class IO."

"Pathname represents the name of a file or directory on the
filesystem, but not the file itself."

Consequently, Pathname#read can only do the same as File::read (i.e.
the class method!), which cannot have an idea of a current read
position either. For subsequent reading of portions of a file you need
to open the file, which can be done with File::open or Pathname#open.

Kind regards

robert

···

On Thu, Jun 1, 2017 at 5:18 PM, Andy Jones <Andy.Jones@jameshall.co.uk> wrote:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/