IO#lineno always returns 0

e$B@DLZ$G$9!#e(B

HEAD e$B$Ge(B IO#lineno e$B$,>o$Ke(B 0 e$B$rJV$9$h$&$K$J$C$F$$$^$9!#e(B

~/c/bitclust % rm -f tmp
~/c/bitclust % echo a > tmp
~/c/bitclust % echo b >> tmp
~/c/bitclust % echo c >> tmp
~/c/bitclust % cat t
File.open(‘tmp’) {|f|
#f.each do |line| # each e$B$G$bF1$8e(B
while line = f.gets
p f.lineno
end
}
~/c/bitclust % ruby -v t
ruby 1.9.0 (2006-12-31 patchlevel 0) [x86_64-linux]
0
0
0

e$B$3$N%j%S%8%g%s$Oe(B YARV e$B$rF~$l$kD>A0$N$d$D$G$9$,!"e(BYARV
e$B8e$bF1$8$G$9!#e(B

tukumo:~ % ruby -ve ‘File.open(ARGV[0]){|f| f.each{|line| p f.lineno }}’
tmp.tar | head
ruby 1.9.0 (2007-01-02 patchlevel 0) [x86_64-linux]
0
0
0
0
0
0
0
0
0

e$B$J$+$@$G$9!#e(B

At Wed, 3 Jan 2007 10:25:19 +0900,
Minero A. wrote in [ruby-dev:30065]:

HEAD e$B$Ge(B IO#lineno e$B$,>o$Ke(B 0 e$B$rJV$9$h$&$K$J$C$F$$$^$9!#e(B

e$B$A$g$&$I0l9T$ND9$5$HF1$8e(Blimite$B$r;XDj$7$?$H$-$Ke(Blinenoe$B$,?J$s$G$$$^$;$s!#e(B

$ yes hello | ./miniruby.exe -e ‘3.times{gets(6);p STDIN.lineno}’
0
0
0

$ ./miniruby.exe -e ‘open(ARGV[0]){|f|3.times{s = f.gets(39); p
[f.lineno, s]}}’ …/src/version.h
[1, “#define RUBY_VERSION “1.9.0”\n”]
[1, “#define RUBY_RELEASE_DATE “2007-01-04”\n”]
[2, “#define RUBY_VERSION_CODE 190\n”]

Index: io.c

— io.c (revision 11475)
+++ io.c (working copy)
@@ -1644,7 +1644,7 @@
}

 if (!NIL_P(str)) {
  • if (limit != 0) {
  • if (c == EOF || c == delim) {
    fptr->lineno++;
    lineno = INT2FIX(fptr->lineno);
    }
    @@ -1740,7 +1740,7 @@
    }

    if (!NIL_P(str)) {

  • if (limit != 0) {
  • if (c == EOF || c == delim) {
    fptr->lineno++;
    lineno = INT2FIX(fptr->lineno);
    }

e$B$J$+$@$G$9!#e(B

At Fri, 5 Jan 2007 14:15:47 +0900,
Nobuyoshi N. wrote in [ruby-dev:30081]:

e$B$A$g$&$I0l9T$ND9$5$HF1$8e(Blimite$B$r;XDj$7$?$H$-$Ke(Blinenoe$B$,?J$s$G$$$^$;$s!#e(B

Index: io.c

— io.c (revision 11475)
+++ io.c (working copy)

e$B@57nAa!9$&$C$+$j%3%s%Q%$%k$,$H$*$i$J$$%Q%C%A$r=P$7$F$7$^$$$^$7$?!#e(B

e$B$D$$$G$K!"e(BIO#readlinese$B$de(BIO#each_linee$B$GKh2se(Brse$B$de(Blimite$B$r%A%’%C%/$7$F$$$ke(B
e$B$N$,5$$K$J$C$?$N$G!"J,N%$7$F$_$^$7$?!#e(B

Index: io.c

— io.c (revision 11479)
+++ io.c (working copy)
@@ -1637,13 +1637,17 @@ rb_io_getline_fast(OpenFile *fptr, unsig
{
VALUE str = Qnil;

  • int c;
  • int c, nolimit = 0;

    for (;:wink: {
    c = appendline(fptr, delim, &str, &limit);

  • if (c == EOF || c == delim || limit == 0) break;
  • if (c == EOF || c == delim) break;

  • if (limit == 0) {

  •  nolimit = 1;
    
  •  break;
    
  • }
    }

    if (!NIL_P(str)) {

  • if (limit != 0) {
  • if (!nolimit) {
    fptr->lineno++;
    lineno = INT2FIX(fptr->lineno);
    @@ -1663,10 +1667,8 @@ rscheck(const char *rsptr, long rslen, V
    }

-static VALUE
-rb_io_getline(int argc, VALUE *argv, VALUE io)
+static void
+getline_args(int argc, VALUE *argv, VALUE *rsp, long *limit)
{

  • VALUE rs, lim, str = Qnil;
  • OpenFile *fptr;
  • long limit;
  • VALUE lim, rs;

    if (argc == 0) {
    @@ -1688,5 +1690,14 @@ rb_io_getline(int argc, VALUE *argv, VAL
    }
    }

  • limit = NIL_P(lim) ? 0 : NUM2LONG(lim);
  • *rsp = rs;
  • *limit = NIL_P(lim) ? -1L : NUM2LONG(lim);
    +}

+static VALUE
+rb_io_getline_1(VALUE rs, long limit, VALUE io)
+{

  • VALUE str = Qnil;

  • OpenFile *fptr;

  • int nolimit = 0;

    GetOpenFile(io, fptr);
    @@ -1696,5 +1707,5 @@ rb_io_getline(int argc, VALUE *argv, VAL
    if (RSTRING_LEN(str) == 0) return Qnil;
    }

  • else if (!NIL_P(lim) && limit == 0) {
  • else if (limit == 0) {
    return rb_str_new(0,0);
    }
    @@ -1730,5 +1741,8 @@ rb_io_getline(int argc, VALUE *argv, VAL
    rsptr, rslen) == 0) break;
    }
  •  if (limit == 0) break;
    
  •  if (limit == 0) {
    
  • nolimit = 1;
  • break;
  •  }
    
    }

@@ -1741,5 +1755,5 @@ rb_io_getline(int argc, VALUE *argv, VAL

 if (!NIL_P(str)) {
  • if (limit != 0) {
  • if (!nolimit) {
    fptr->lineno++;
    lineno = INT2FIX(fptr->lineno);
    @@ -1751,4 +1765,14 @@ rb_io_getline(int argc, VALUE *argv, VAL
    }

+static VALUE
+rb_io_getline(int argc, VALUE *argv, VALUE io)
+{

  • VALUE rs;
  • long limit;
  • getline_args(argc, argv, &rs, &limit);
  • return rb_io_getline_1(rs, limit, io);
    +}

VALUE
rb_io_gets(VALUE io)
@@ -1913,8 +1937,10 @@ static VALUE
rb_io_readlines(int argc, VALUE *argv, VALUE io)
{

  • VALUE line, ary;
  • VALUE line, ary, rs;

  • long limit;

  • getline_args(argc, argv, &rs, &limit);
    ary = rb_ary_new();

  • while (!NIL_P(line = rb_io_getline(argc, argv, io))) {
  • while (!NIL_P(line = rb_io_getline_1(rs, limit, io))) {
    rb_ary_push(ary, line);
    }
    @@ -1949,8 +1975,10 @@ static VALUE
    rb_io_each_line(int argc, VALUE *argv, VALUE io)
    {
  • VALUE str;
  • VALUE str, rs;

  • long limit;

    RETURN_ENUMERATOR(io, argc, argv);

  • while (!NIL_P(str = rb_io_getline(argc, argv, io))) {
  • getline_args(argc, argv, &rs, &limit);
  • while (!NIL_P(str = rb_io_getline_1(rs, limit, io))) {
    rb_yield(str);
    }

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:30085] Re: IO#lineno always returns 0”
on Fri, 5 Jan 2007 17:49:10 +0900, “Nobuyoshi N.”
[email protected] writes:

|e$B$D$$$G$K!"e(BIO#readlinese$B$de(BIO#each_linee$B$GKh2se(Brse$B$de(Blimite$B$r%A%'%C%/$7$F$$$ke(B
|e$B$N$,5$$K$J$C$?$N$G!"J,N%$7$F$_$^$7$?!#e(B

e$B%3%_%C%H$7$F$/$@$5$$e(B(trunke$B$Ke(B)e$B!#e(B