2008/11/16 Chad Perrin <perrin / apotheon.com>:
> On Sun, Nov 16, 2008 at 08:36:50PM +0900, Robert Klemme wrote:
>> On 15.11.2008 20:51, Michael Guterl wrote:
>> >On Sat, Nov 15, 2008 at 2:08 PM, Chad Perrin <perrin / apotheon.com> wrote:
>>
>> >>The class is specced here, with the screen scrolled to where IO#lineno
>> >>and IO#lineno= are listed:
>> >>
>> >>   http://ruby-doc.org/core/classes/IO.html#M002289
>> >>
>> >The description of the method is somewhat ambiguous if you ask me.
>>
>> I don't think so.
>
> The more I look at it, the more ambiguous it appears to be.

That's the usual effect of staring at a sentence for too long. :-)  Relax.

>> >------------------------------------------------------------- IO#lineno=
>> >     ios.lineno = integer    => integer
>> >------------------------------------------------------------------------
>> >     Manually sets the current line number to the given value. +$.+ is
>> >     updated only on the next read.
>>
>> There is no talk about read position in the file - just about "current
>> line number".  Also:
>
> . . . which, to someone who isn't assuming "line number" is just a
> magical number plucked out of the air, makes it sound like it moves the
> read position to a line whose ordinal position is that of the specified
> line number.  In other words, that's how it "sounded" to me.

Yes, but the example makes it pretty clear that this is not the way it is:

>> >        f = File.new("testfile")
>> >        f.gets                     #=> "This is line one\n"
>> >        $.                         #=> 1
>> >        f.lineno = 1000
>> >        f.lineno                   #=> 1000
>> >        $. # lineno of last read   #=> 1
>> >        f.gets                     #=> "This is line two\n"
>> >        $. # lineno of last read   #=> 1001
>>
>> The sample makes it very clear that the read position is not affected by
>> lineno= because file reading obviously continues at the position where
>> it was before.
>
> It only makes that clear if you assume a lot of things about what's in
> the file in question.  I can see now, in retrospect, how you came to that
> conclusion -- but the fact that the second use of `f.gets` returns "This
> is line two\n" doesn't *necessarily* mean that the return value is from
> the second line of the file.

Of course not.  But what sense would it make to create a file with a
different content that would return "This is line two\n" when
explaining how lineno= works?  The most obvious explanation is that
someone created a file where "This is line two" is actually placed in
the second line to demonstrate the non effect on file position.

>  I read it, initially, as meaning that
> whatever line of the file it was, it just happened to say "This is line
> two\n" because that made for some convenient text to have in the example.

Actually I believe the other interpretation is much more
straightforward and reasonable.

> Since the contents of the file were not made clear in advance, the
> assumption that only the second line of the file can possibly say "This
> is line two\n" does not clarify anything for the reader except by
> accident.  It could just mean "This is the second line of output from
> this code."

See above.  IMHO only a bit application of common sense will show you
that your reasoning goes a bit astray here - although from a formal
point of view you are right.

>> >I would think if it had the behavior you described, the second time
>> >f.gets is called, we would see: "This is line one thousand and one\n"
>> >not "This is line two\n"
>>
>> Right (if by "you" you do not mean Tim, somehow part of the thread is
>> missing in Usenet).
>
> I don't see why everyone has to assume that the second line of the file
> necessarily contains the text "This is line two\n".  It's really very
> ambiguous.

... for you.

> If you want a program that outputs "This is line one\nThis is
> line two\n", and for some reason lines 0 and 1000 of the file contain
> "This is line one\n" and "This is line two\n" respectively, the alternate
> interpretation of the way the method works makes *perfect* sense.

Formally speaking yes, with a bit of common sense, no.

> What *doesn't* make any sense to me is the idea that, for some reason,
> it's important and common enough an operation to misnumber line numbers
> that there has to be a `lineno=` method that counterfeits line numbers.
> What the hell is the point of that?  Please explain that to me.

I do not know this.  IO#lineno= can help implementing ARGF although it
is not needed.  Maybe ARGF is completely implemented in Ruby and
delegates to C code for the IO handling - including line counting. In
that case it's handy to have this setter so you can offset the line
number of the next opened file.

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end