# IO#lineno always returns 0

**URL:** <https://www.ruby-forum.com/t/io-lineno-always-returns-0/81019>\
**Category:** Development\
**Created:** [January 3, 2007, 2:26am UTC](https://www.ruby-forum.com/t/io-lineno-always-returns-0/81019 "2007-01-03T02:26:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Minero\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Minero\_A](https://www.ruby-forum.com/u/Minero_A)\
**Post date:** [January 3, 2007, 2:26am UTC](https://www.ruby-forum.com/t/io-lineno-always-returns-0/81019/1 "2007-01-03T02:26:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Minero\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Minero\_A](https://www.ruby-forum.com/u/Minero_A)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/io-lineno-always-returns-0/81019/2 "2007-01-06T19:30:27Z")

</div>

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 (limit != 0) {

- if (c == EOF || c == delim) {  
fptr-\>lineno++;  
lineno = INT2FIX(fptr-\>lineno);  
}

---

<div class="post-metadata">

**Author:** ![Minero\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Minero\_A](https://www.ruby-forum.com/u/Minero_A)\
**Post date:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/io-lineno-always-returns-0/81019/3 "2007-01-19T16:29:21Z")

</div>

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;

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

- if (c == EOF || c == delim) break;

- if (limit == 0) {

- 

```
 nolimit = 1;

```

- 

```
 break;

```

- }  
}

- 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;

- 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;

- 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;

- 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);  
}

---

<div class="post-metadata">

**Author:** ![Minero\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Minero\_A](https://www.ruby-forum.com/u/Minero_A)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/io-lineno-always-returns-0/81019/4 "2007-01-19T16:31:46Z")

</div>

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.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
