Confusion in value of $

What is going on here? The value of $. is different depending on
whether the input is coming from a file list or a redirect - see
below:

$ od -c test.txt
0000000 1 \n 2 \n 3 \n 4 \n 5 \n
0000012
$ cat test.rb
p $<.readlines
p $.
$ ruby test.rb test.txt
[“1\n”, “2\n”, “3\n”, “4\n”, “5\n”]
5
$ ruby test.rb <test.txt
[“1\n”, “2\n”, “3\n”, “4\n”, “5\n”]
6
$

According to Programming Ruby, the value of $. is the line number of
the last line read from the current input file. I can’t think why the
value should be 6 in the case of the redirect, when there are only 5
lines in the file.

haldane

Hi,

What is going on here? The value of $. is different depending on
whether the input is coming from a file list or a redirect - see
below:

$ od -c test.txt
0000000 1 \n 2 \n 3 \n 4 \n 5 \n
0000012
$ cat test.rb
p $<.readlines
p $.
$ ruby test.rb test.txt
[“1\n”, “2\n”, “3\n”, “4\n”, “5\n”]
5
$ ruby test.rb <test.txt
[“1\n”, “2\n”, “3\n”, “4\n”, “5\n”]
6
$

You’ve found a bug. Thank you.

						matz.

— io.c 31 Jan 2003 04:00:16 -0000 1.183
+++ io.c 3 Feb 2003 14:49:23 -0000
@@ -3039,5 +3039,6 @@ argf_getline(argc, argv)
}

  • gets_lineno++;
  • lineno = INT2FIX(gets_lineno);
···

In message “Confusion in value of $.” on 03/02/03, haldane jbshaldane@hotmail.com writes:

  • if (!NIL_P(line)) {
  • gets_lineno++;
  • lineno = INT2FIX(gets_lineno);
  • }
    return line;