Initial value of $


#1

Yuguie$B$G$9!#e(B

e$B8=:_$Ne(Btrunk(r19950) e$B$Ge(B ruby -e ‘p $.’
e$B$9$k$He(B89e$B$,JV$j$^$9!#e(B
gem_prelude.rbe$BFb$Ge(Bgeme$B$N=i4|2=%U%!%$%k$re(Brequiree$B$7$F$$$k$?$a$G$9!#e(B

e$B2?$+e(BARGVe$B$+$iB>$N$b$N$rFI$_9~$a$P%/%j%"$5$l$k$N$G!"$=$N0UL#$G$O<B32$O$"$je(B
e$B$^$;$s!#$?$@$7!"e(B
(1) e$BFbIt<BAu$,L5MQ$KI==P$7$F$$$k$N$O5$;}$A0-$$e(B
(2) 0e$B$G=i4|2=$5$l$?JQ?t$r;H$$$?$$%3!<%I%4%k%U%!!<$,Ha$7$`e(B

e$B$H$$$&LdBj$,$"$j$^$9!#$5$F!“e(B$.e$B$N=i4|CM$O$I$&$”$k$Y$-$G$7$g$&$+!#e(B
e$B$^$?!"e(Brequiree$B$,e(B$.e$B$rJQ99$9$k$N$OK>$^$7$$$G$7$g$&$+!#e(B


#2

In article removed_email_address@domain.invalid,
“Yugui (Yuki S.)” removed_email_address@domain.invalid writes:

gem_prelude.rbe$BFb$Ge(Bgeme$B$N=i4|2=%U%!%$%k$re(Brequiree$B$7$F$$$k$?$a$G$9!#e(B

RubyGems e$B$r$$$l$?$H$-$K$^$D$b$H$5$s$,!"%G%U%)%k%H$G$Oe(B
require/load e$B$7$J$$$3$H$rMW5a$7$F$^$7$?$,!“K:$l$i$l$?$s$G$9e(B
e$B$+$M!#e(B(e$B$”$k$$$O!"$^$D$b$H$5$s$,MW5a$r<h$j2<$2$?$+e(B)


#3

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

In message “Re: [ruby-dev:36937] initial value of $.”
on Sun, 26 Oct 2008 22:36:55 +0900, “Yugui (Yuki S.)”
removed_email_address@domain.invalid writes:

|e$B8=:_$Ne(Btrunk(r19950) e$B$Ge(B ruby -e ‘p $.’ e$B$9$k$He(B89e$B$,J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j$^$9$M!#7nMK$K;~4V$,<h$l$?$i=$e(B
e$B@5$7$^$9$,!"@h$K$I$J$?$+$,D>$7$F$/$@$5$C$F$bJ86g$O8@$$$^$;$s!#e(B
e$B$`$7$mBgJQ46<U$7$^$9!#e(B

a. requiree$B$H$$$&$+e(Bparse.ye$B$,e(Bgetse$B0J30$r;H$&e(B
b. rb_f_require()e$B$Ge(B$.e$B$rJ]B8$9$ke(B
c. $.e$B$re(B$_,$~e$B$HF1$8$/%a%=%C%Ie(B/e$B%9%l%C%I%9%3!<%W$K$9$ke(B

e$B$J$I$r9M$($?$s$G$9$,!"$I$&$7$^$7$g$&$+$M$’!#e(B


#5

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

In message “Re: [ruby-dev:36939] Re: initial value of $.”
on Sun, 26 Oct 2008 22:40:42 +0900, Tanaka A. removed_email_address@domain.invalid
writes:

|> gem_prelude.rbe$BFb$Ge(Bgeme$B$N=i4|2=%U%!%$%k$re(Brequiree$B$7$F$$$k$?$a$G$9!#e(B
|
|RubyGems e$B$r$$$l$?$H$-$K$^$D$b$H$5$s$,!"%G%U%)%k%H$G$Oe(B
|require/load e$B$7$J$$$3$H$rMW5a$7$F$^$7$?$,!“K:$l$i$l$?$s$G$9e(B
|e$B$+$M!#e(B(e$B$”$k$$$O!"$^$D$b$H$5$s$,MW5a$r<h$j2<$2$?$+e(B)

e$B$"$s$^$j6/$/<gD%$7$J$+$C$?$N$G!“K:$l$i$l$?$s$8$c$J$$$G$9$+$M!#e(B
e$BJ*K:$l$,7c$7$$;d$G$9$,!”>/$J$/$H$b<h$j2<$2$?$3$H$r3P$($F$O$$e(B
e$B$^$;$s!#$^$"!"e(B$.e$B$,JQ2=$7$5$($7$J$1$l$P<B32$O$J$$$N$+$b$7$l$^e(B
e$B$;$s$,!#e(B


#6

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

In message “Re: [ruby-dev:36947] Re: initial value of $.”
on Mon, 27 Oct 2008 11:31:27 +0900, Nobuyoshi N.
removed_email_address@domain.invalid writes:

|At Mon, 27 Oct 2008 00:56:36 +0900,
|Yukihiro M. wrote in [ruby-dev:36942]:
|> $.e$B$N=i4|CM$Oe(B0e$B$G$"$k$Y$-$G!“e(Brequiree$B$,e(B$.e$B$rJQ99$9$Y$-$G$O$J$$$Ge(B
|> e$B$7$g$&!#$3$l$O=$@5$NI,MW$,$”$j$^$9$M!#7nMK$K;~4V$,<h$l$?$i=$e(B
|> e$B@5$7$^$9$,!"@h$K$I$J$?$+$,D>$7$F$/$@$5$C$F$bJ86g$O8@$$$^$;$s!#e(B
|> e$B$`$7$mBgJQ46<U$7$^$9!#e(B
|
|a. requiree$B$H$$$&$+e(Bparse.ye$B$,e(Bgetse$B0J30$r;H$&e(B
|b. rb_f_require()e$B$Ge(B$.e$B$rJ]B8$9$ke(B
|c. $.e$B$re(B$_,$~e$B$HF1$8$/%a%=%C%Ie(B/e$B%9%l%C%I%9%3!<%W$K$9$ke(B
|
|e$B$J$I$r9M$($?$s$G$9$,!"$I$&$7$^$7$g$&$+$M$’!#e(B

e$B;d$Oe(Bbe$B$r9M$($F$^$7$?!#@53N$K$Oe(Brb_loade$B$G$9$+!#e(B


#7

Yuguie$B$G$9!#e(B

Yukihiro M. e$B$5$s$O=q$-$^$7$?e(B:

. requiree$B$H$$$&$+e(Bparse.ye$B$,e(Bgetse$B0J30$r;H$&e(B
|b. rb_f_require()e$B$Ge(B$.e$B$rJ]B8$9$ke(B
|c. $.e$B$re(B$_,$~e$B$HF1$8$/%a%=%C%Ie(B/e$B%9%l%C%I%9%3!<%W$K$9$ke(B
|
|e$B$J$I$r9M$($?$s$G$9$,!"$I$&$7$^$7$g$&$+$M$’!#e(B

e$B;d$Oe(Bbe$B$r9M$($F$^$7$?!#@53N$K$Oe(Brb_loade$B$G$9$+!#e(B

e$BK\7o$Oe(Bbe$B$GCfED$5$s$,D>$7$F$/$@$5$$$^$7$?$,!"e(Bloade$B=*N;;~$Ke(B$.e$B$rI|85$7$F$^e(B
e$B$9!#$G$9$N$G!"0MA3$H$7$Fe(Bloade$B$He(Brippere$B$,JB9T$KF0$$$?$ie(B$.e$B$K2?$,5/$-$k$N$+$He(B
e$B$F$bIT0B$G$9!#e(B

e$B$3$l$O$I$&$9$Y$-$J$N$+!":#8e8!F$$7$F$$$/$Y$-$G$7$g$&!#e(B


#8

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

In message “Re: [ruby-dev:36953] Re: initial value of $.”
on Mon, 27 Oct 2008 16:26:58 +0900, Yukihiro M.
removed_email_address@domain.invalid writes:

|e$B$$$d!"D>@\E*$J860x$Oe(Bio.ce$B$Ne(Brb_io_getline_fast()e$B$J$I$GA}2C$5$;e(B
|e$B$F$k$+$i$J$s$G$9$,!"e(BARGFe$B$G$J$$$+$b$7$l$J$$e(Bioe$B$+$i$Ne(Bgetse$B$GA}2Ce(B
|e$B$9$k$N$OL@$i$+$K$*$+$7$$5$$,$7$^$9!#e(B1.8e$B$G$O$=$N$h$&$K$J$C$F$$e(B
|e$B$^$;$s!#e(Br15663e$B$GCfED$5$s$,DI2C$7$?$_$?$$$G$9$,!#e(B

e$B$9$_$^$;$s!"2a5n%m%0$r:F3NG’$7$?$i$3$N5sF0$OCfED$5$s$,DI2C$7e(B
e$B$?$o$1$G$O$J$$$G$9$7!“e(B1.8e$B$G$be(B$.e$B$OA}2C$7$^$9$M!#$”$i$i!#e(B

e$B$G$b!"$3$l$,%P%0$G$"$k$H8@$&0U8+$OJQ$o$j$^$;$s!#e(B


#9

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

In message “Re: [ruby-dev:36952] Re: initial value of $.”
on Mon, 27 Oct 2008 16:09:50 +0900, “Yugui (Yuki S.)”
removed_email_address@domain.invalid writes:

|> e$B;d$Oe(Bbe$B$r9M$($F$^$7$?!#@53N$K$Oe(Brb_loade$B$G$9$+!#e(B
|
|e$BK\7o$Oe(Bbe$B$GCfED$5$s$,D>$7$F$/$@$5$$$^$7$?$,!"e(Bloade$B=*N;;~$Ke(B$.e$B$rI|85$7$F$^e(B
|e$B$9!#$G$9$N$G!“0MA3$H$7$Fe(Bloade$B$He(Brippere$B$,JB9T$KF0$$$?$ie(B$.e$B$K2?$,5/$-$k$N$+$He(B
|e$B$F$bIT0B$G$9!#e(B
|
|e$B$3$l$O$I$&$9$Y$-$J$N$+!”:#8e8!F$$7$F$$$/$Y$-$G$7$g$&!#e(B

e$B$H$$$&$+!":#5$$,$D$$$?$s$G$9$,e(B(e$BCY$$e(B)e$B!“e(Bparsere$B$Oe(Bioe$B$r;XDj$7$Fe(B
getse$B$7$F$$$k$N$K!”$J$<e(BARGFe$B$N9THV9f$G$"$ke(B$.e$B$,A}2C$9$k$N$G$7$ge(B
e$B$&$+!#e(B

e$B$$$d!"D>@\E*$J860x$Oe(Bio.ce$B$Ne(Brb_io_getline_fast()e$B$J$I$GA}2C$5$;e(B
e$B$F$k$+$i$J$s$G$9$,!"e(BARGFe$B$G$J$$$+$b$7$l$J$$e(Bioe$B$+$i$Ne(Bgetse$B$GA}2Ce(B
e$B$9$k$N$OL@$i$+$K$*$+$7$$5$$,$7$^$9!#e(B1.8e$B$G$O$=$N$h$&$K$J$C$F$$e(B
e$B$^$;$s!#e(Br15663e$B$GCfED$5$s$,DI2C$7$?$_$?$$$G$9$,!#e(B

e$B$3$l$O%P%0$G$7$g$&!#$H$$$&$3$H$O!"e(Brb_loade$B$N=$@5$O$=$b$=$bITMWe(B
e$B$H$$$&$3$H$G$O$J$$$+$J!#e(B

                            e$B$^$D$b$He(B e$B$f$-$R$me(B /:|)

#10

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

At Mon, 27 Oct 2008 16:30:19 +0900,
Yukihiro M. wrote in [ruby-dev:36954]:

|e$B$$$d!"D>@\E*$J860x$Oe(Bio.ce$B$Ne(Brb_io_getline_fast()e$B$J$I$GA}2C$5$;e(B
|e$B$F$k$+$i$J$s$G$9$,!"e(BARGFe$B$G$J$$$+$b$7$l$J$$e(Bioe$B$+$i$Ne(Bgetse$B$GA}2Ce(B
|e$B$9$k$N$OL@$i$+$K$*$+$7$$5$$,$7$^$9!#e(B1.8e$B$G$O$=$N$h$&$K$J$C$F$$e(B
|e$B$^$;$s!#e(Br15663e$B$GCfED$5$s$,DI2C$7$?$_$?$$$G$9$,!#e(B

e$B$9$_$^$;$s!"2a5n%m%0$r:F3NG’$7$?$i$3$N5sF0$OCfED$5$s$,DI2C$7e(B
e$B$?$o$1$G$O$J$$$G$9$7!“e(B1.8e$B$G$be(B$.e$B$OA}2C$7$^$9$M!#$”$i$i!#e(B

e$B$G$b!"$3$l$,%P%0$G$"$k$H8@$&0U8+$OJQ$o$j$^$;$s!#e(B

e$B$G$"$l$P!"e(Br19965e$B$Oe(Breverte$B$7$F$3$s$J$H$3$m$G$7$g$&$+!#e(B

IO#getse$B$de(BIO#eache$B!"e(BIO.foreache$B$J$I$Ge(B$.e$B$,99?7$5$l$k$3$H$r4|BT$7$F$$e(B
e$B$k%W%m%0%i%`$O!"F0$+$J$/$J$k2DG=@-$,9b$$$G$9$,!#e(B

Index: io.c

— io.c (revision 19966)
+++ io.c (working copy)
@@ -132,5 +132,4 @@ struct argf {
int gets_lineno;
int init_p, next_p;

  • VALUE lineno;
    VALUE argv;
    char *inplace;
    @@ -1079,7 +1078,4 @@ rb_io_rewind(VALUE io)
    GetOpenFile(io, fptr);
    if (io_seek(fptr, 0L, 0) < 0) rb_sys_fail_path(fptr->pathv);
  • if (io == ARGF.current_file) {
  • ARGF.gets_lineno -= fptr->lineno;
  • }
    fptr->lineno = 0;

@@ -2085,5 +2081,4 @@ rb_io_getline_fast(rb_io_t *fptr, rb_enc
ENC_CODERANGE_SET(str, cr);
fptr->lineno++;

  • ARGF.lineno = INT2FIX(fptr->lineno);
    return str;
    }
    @@ -2220,5 +2215,4 @@ rb_io_getline_1(VALUE rs, long limit, VA
    if (!nolimit) {
    fptr->lineno++;
  •  ARGF.lineno = INT2FIX(fptr->lineno);
    
    }
    }
    @@ -5805,5 +5799,4 @@ argf_mark(void *ptr)
    rb_gc_mark(p->filename);
    rb_gc_mark(p->current_file);
  • rb_gc_mark(p->lineno);
    rb_gc_mark(p->argv);
    rb_gc_mark(p->encs.ecopts);
    @@ -5822,5 +5815,5 @@ argf_init(struct argf *p, VALUE v)
    p->filename = Qnil;
    p->current_file = Qnil;
  • p->lineno = INT2FIX(0);
  • p->gets_lineno = 0;
    p->argv = v;
    }
    @@ -5864,5 +5857,4 @@ argf_set_lineno(VALUE argf, VALUE val)
    {
    ARGF.gets_lineno = NUM2INT(val);
  • ARGF.lineno = INT2FIX(ARGF.gets_lineno);
    return Qnil;
    }
    @@ -5871,5 +5863,5 @@ static VALUE
    argf_lineno(VALUE argf)
    {
  • return ARGF.lineno;
  • return INT2FIX(ARGF.gets_lineno);
    }

@@ -6050,5 +6042,4 @@ argf_getline(int argc, VALUE *argv, VALU
if (!NIL_P(line)) {
ARGF.gets_lineno++;

  • ARGF.lineno = INT2FIX(ARGF.gets_lineno);
    }
    return line;
    @@ -6059,5 +6050,5 @@ argf_lineno_getter(ID id, VALUE *var)
    {
    VALUE argf = *var;
  • return ARGF.lineno;
  • return INT2FIX(ARGF.gets_lineno);
    }

@@ -6066,7 +6057,5 @@ argf_lineno_setter(VALUE val, ID id, VAL
{
VALUE argf = *var;

  • int n = NUM2INT(val);
  • ARGF.gets_lineno = n;
  • ARGF.lineno = INT2FIX(n);
  • ARGF.gets_lineno = NUM2INT(val);
    }

@@ -6145,5 +6134,4 @@ rb_gets(void)
if (!NIL_P(line)) {
ARGF.gets_lineno++;

  • ARGF.lineno = INT2FIX(ARGF.gets_lineno);
    }

@@ -7756,9 +7744,18 @@ static VALUE
argf_rewind(VALUE argf)
{

  • rb_io_t *fptr;
  • VALUE io, ret;
  • int lineno = 0;
  • if (!next_argv()) {
    rb_raise(rb_eArgError, “no stream to rewind”);
    }
    ARGF_FORWARD(0, 0);
  • return rb_io_rewind(ARGF.current_file);
  • io = ARGF.current_file;
  • GetOpenFile(io, fptr);
  • lineno = fptr->lineno;
  • ret = rb_io_rewind(io);
  • ARGF.gets_lineno -= lineno;
  • return ret;
    }

Index: test/ruby/test_io.rb

— test/ruby/test_io.rb (revision 19966)
+++ test/ruby/test_io.rb (working copy)
@@ -800,18 +800,18 @@ class TestIO < Test::Unit::TestCase
ruby("-e", <<-SRC, t.path) do |f|
open(ARGV[0]) do |f|

  •    p $.
    
  •    f.gets; p $.
    
  •    f.gets; p $.
    
  •    f.lineno = 1000; p $.
    
  •    f.gets; p $.
    
  •    f.gets; p $.
    
  •    f.rewind; p $.
    
  •    f.gets; p $.
    
  •    f.gets; p $.
    
  •    f.gets; p $.
    
  •    f.gets; p $.
    
  •    p f.lineno
    
  •    f.gets; p f.lineno
    
  •    f.gets; p f.lineno
    
  •    f.lineno = 1000; p f.lineno
    
  •    f.gets; p f.lineno
    
  •    f.gets; p f.lineno
    
  •    f.rewind; p f.lineno
    
  •    f.gets; p f.lineno
    
  •    f.gets; p f.lineno
    
  •    f.gets; p f.lineno
    
  •    f.gets; p f.lineno
     end
    
    SRC
  •  assert_equal("0,1,2,2,1001,1001,1001,1,2,3,3", 
    

f.read.chomp.gsub("\n", “,”))

  •  assert_equal("0,1,2,1000,1001,1001,0,1,2,3,3", 
    

f.read.chomp.gsub("\n", “,”))
end

@@ -822,9 +822,9 @@ class TestIO < Test::Unit::TestCase
w.close
end, proc do |r|

  •  r.gets; assert_equal(1, $.)
    
  •  r.gets; assert_equal(2, $.)
    
  •  r.lineno = 1000; assert_equal(2, $.)
    
  •  r.gets; assert_equal(1001, $.)
    
  •  r.gets; assert_equal(1001, $.)
    
  •  r.gets; assert_equal(1, r.lineno)
    
  •  r.gets; assert_equal(2, r.lineno)
    
  •  r.lineno = 1000; assert_equal(1000, r.lineno)
    
  •  r.gets; assert_equal(1001, r.lineno)
    
  •  r.gets; assert_equal(1001, r.lineno)
    
    end)
    end
    @@ -837,8 +837,8 @@ class TestIO < Test::Unit::TestCase
    w.close
    end, proc do |r|
  •  r.readline; assert_equal(1, $.)
    
  •  r.readline; assert_equal(2, $.)
    
  •  r.lineno = 1000; assert_equal(2, $.)
    
  •  r.readline; assert_equal(1001, $.)
    
  •  r.readline; assert_equal(1, r.lineno)
    
  •  r.readline; assert_equal(2, r.lineno)
    
  •  r.lineno = 1000; assert_equal(1000, r.lineno)
    
  •  r.readline; assert_equal(1001, r.lineno)
     assert_raise(EOFError) { r.readline }
    
    end)
    Index: test/ruby/test_argf.rb
    ===================================================================
    — test/ruby/test_argf.rb (revision 19966)
    +++ test/ruby/test_argf.rb (working copy)
    @@ -63,13 +63,13 @@ class TestArgf < Test::Unit::TestCase
    a.rewind
    b.rewind
  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["1", 1, 
    

“1”, 3]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["2", 2, 
    

“2”, 4]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["3", 3, 
    

“3”, 5]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["4", 4, 
    

“4”, 6]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, 
    

“5”, 7]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["1", 1, 
    

“1”, 1]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["2", 2, 
    

“2”, 2]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["3", 3, 
    

“3”, 3]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["4", 4, 
    

“4”, 4]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, 
    

“5”, 5]
a.rewind
b.rewind

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, 
    

“5”, 8]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["6", 6, 
    

“6”, 9]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, 
    

“5”, 5]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["6", 6, 
    

“6”, 6]
SRC
expected = src.scan(/#=> (.+)/).flatten
@@ -91,8 +91,8 @@ class TestArgf < Test::Unit::TestCase
a.gets; p $. #=> 2
a.gets; p $. #=> 3

  •  a.rewind; p $.  #=> 3
    
  •  a.rewind; p $.  #=> 2
     a.gets; p $.  #=> 3
     a.gets; p $.  #=> 4
    
  •  a.rewind; p $.  #=> 4
    
  •  a.rewind; p $.  #=> 2
     a.gets; p $.  #=> 3
     a.lineno = 1000; p $.  #=> 1000
    

@@ -112,14 +112,14 @@ class TestArgf < Test::Unit::TestCase
src = <<-SRC
a = ARGF.dup

  •  a.gets; p $.  #=> 1
    
  •  a.gets; p $.  #=> 2
    
  •  a.gets; p $.  #=> 1
    
  •  a.rewind; p $.  #=> 1
    
  •  a.gets; p $.  #=> 1
    
  •  a.gets; p $.  #=> 2
    
  •  a.gets; p $.  #=> 1
    
  •  a.lineno = 1000; p $.  #=> 1
    
  •  a.gets; p $.  #=> 2
    
  •  a.gets; p $.  #=> 2
    
  •  a.gets; p $.  #=> 0
    
  •  a.gets; p $.  #=> 0
    
  •  a.gets; p $.  #=> 0
    
  •  a.rewind; p $.  #=> 0
    
  •  a.gets; p $.  #=> 0
    
  •  a.gets; p $.  #=> 0
    
  •  a.gets; p $.  #=> 0
    
  •  a.lineno = 1000; p $.  #=> 0
    
  •  a.gets; p $.  #=> 0
    
  •  a.gets; p $.  #=> 0
     $. = 2000
     a.gets; p $.  #=> 2000

#11

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

In message “Re: [ruby-dev:36955] Re: initial value of $.”
on Mon, 27 Oct 2008 18:25:01 +0900, Nobuyoshi N.
removed_email_address@domain.invalid writes:

|> e$B$9$_$^$;$s!“2a5n%m%0$r:F3NG’$7$?$i$3$N5sF0$OCfED$5$s$,DI2C$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$K$/$$>e$K!“LdBj$b@8$8$K$/$$BE6(E@$+$b$7$l$^$;$s!#$,!”:#e(B
e$B$N$^$^$G$bJL$K:$$C$F$J$$$N$b;v<B$J$N$G!"e(Brb_loade$B$N=$@5$@$1$Ge(B
e$B==J,$H8@$&$N$b$b$C$H$b$J5$$,$7$^$9!#e(B

e$BB>$N%a%=%C%I$H$N43>D$rKI$2$k$H$$$&E@$G$Oe(Bsvare$B$K$9$k$H$$$&$N$b$$$$e(B
e$B$N$G$O$J$$$+$H;W$$$^$9$,!"%9%l%C%I%m!<%+%k$K$9$k%Q%C%A$G$9!#e(B

e$B$D$$$G$K!"e(Brewinde$B$N$H$-$Ne(B$.e$B$NF0:n$O>/$7$*$+$7$$$h$&$J5$$b$7$^$9!#e(B

Index: io.c

— io.c (revision 19983)
+++ io.c (working copy)
@@ -122,5 +122,5 @@ VALUE rb_default_rs;
static VALUE argf;

-static ID id_write, id_read, id_getc, id_flush, id_readpartial;
+static ID id_write, id_read, id_getc, id_flush, id_readpartial,
id_lineno;
static VALUE sym_mode, sym_perm, sym_extenc, sym_intenc, sym_encoding,
sym_open_args;
static VALUE sym_textmode, sym_binmode;
@@ -132,5 +132,4 @@ struct argf {
int gets_lineno;
int init_p, next_p;

  • VALUE lineno;
    VALUE argv;
    char *inplace;
    @@ -198,4 +197,18 @@ static int max_file_descriptor = NOFILE;
    #define rb_sys_fail_path(path) rb_sys_fail(NIL_P(path) ? 0 :
    RSTRING_PTR(path))

+static VALUE
+get_last_lineno(void)
+{

  • VALUE n = rb_thread_local_aref(rb_thread_current(), id_lineno);
  • if (NIL_P(n)) return INT2FIX(0);
  • return n;
    +}

+static VALUE
+set_last_lineno(int num)
+{

  • return rb_thread_local_aset(rb_thread_current(), id_lineno,
    INT2FIX(num));
    +}

#if defined(_WIN32)
#define is_socket(fd, path) rb_w32_is_socket(fd)
@@ -1079,7 +1092,4 @@ rb_io_rewind(VALUE io)
GetOpenFile(io, fptr);
if (io_seek(fptr, 0L, 0) < 0) rb_sys_fail_path(fptr->pathv);

  • if (io == ARGF.current_file) {
  • ARGF.gets_lineno -= fptr->lineno;
  • }
    fptr->lineno = 0;

@@ -2085,5 +2095,5 @@ rb_io_getline_fast(rb_io_t *fptr, rb_enc
ENC_CODERANGE_SET(str, cr);
fptr->lineno++;

  • ARGF.lineno = INT2FIX(fptr->lineno);
  • set_last_lineno(fptr->lineno);
    return str;
    }
    @@ -2220,5 +2230,5 @@ rb_io_getline_1(VALUE rs, long limit, VA
    if (!nolimit) {
    fptr->lineno++;
  •  ARGF.lineno = INT2FIX(fptr->lineno);
    
  •  set_last_lineno(fptr->lineno);
    
    }
    }
    @@ -5805,5 +5815,4 @@ argf_mark(void *ptr)
    rb_gc_mark(p->filename);
    rb_gc_mark(p->current_file);
  • rb_gc_mark(p->lineno);
    rb_gc_mark(p->argv);
    rb_gc_mark(p->encs.ecopts);
    @@ -5822,5 +5831,5 @@ argf_init(struct argf *p, VALUE v)
    p->filename = Qnil;
    p->current_file = Qnil;
  • p->lineno = INT2FIX(0);
  • p->gets_lineno = 0;
    p->argv = v;
    }
    @@ -5863,6 +5872,7 @@ static VALUE
    argf_set_lineno(VALUE argf, VALUE val)
    {
  • ARGF.gets_lineno = NUM2INT(val);
  • ARGF.lineno = INT2FIX(ARGF.gets_lineno);
  • int n = NUM2INT(val);
  • ARGF.gets_lineno = n;
  • set_last_lineno(n);
    return Qnil;
    }
    @@ -5871,5 +5881,5 @@ static VALUE
    argf_lineno(VALUE argf)
    {
  • return ARGF.lineno;
  • return INT2FIX(ARGF.gets_lineno);
    }

@@ -6050,5 +6060,5 @@ argf_getline(int argc, VALUE *argv, VALU
if (!NIL_P(line)) {
ARGF.gets_lineno++;

  • ARGF.lineno = INT2FIX(ARGF.gets_lineno);
  • set_last_lineno(ARGF.gets_lineno);
    }
    return line;
    @@ -6058,6 +6068,5 @@ static VALUE
    argf_lineno_getter(ID id, VALUE *var)
    {
  • VALUE argf = *var;
  • return ARGF.lineno;
  • return get_last_lineno();
    }

@@ -6068,5 +6077,5 @@ argf_lineno_setter(VALUE val, ID id, VAL
int n = NUM2INT(val);
ARGF.gets_lineno = n;

  • ARGF.lineno = INT2FIX(n);
  • set_last_lineno(n);
    }

@@ -6145,5 +6154,5 @@ rb_gets(void)
if (!NIL_P(line)) {
ARGF.gets_lineno++;

  • ARGF.lineno = INT2FIX(ARGF.gets_lineno);
  • set_last_lineno(ARGF.gets_lineno);
    }

@@ -7756,9 +7765,19 @@ static VALUE
argf_rewind(VALUE argf)
{

  • rb_io_t *fptr;
  • VALUE io, ret;
  • int lineno = 0;
  • if (!next_argv()) {
    rb_raise(rb_eArgError, “no stream to rewind”);
    }
    ARGF_FORWARD(0, 0);
  • return rb_io_rewind(ARGF.current_file);
  • io = ARGF.current_file;
  • GetOpenFile(io, fptr);
  • lineno = fptr->lineno;
  • ret = rb_io_rewind(io);
  • ARGF.gets_lineno -= lineno;
  • set_last_lineno(ARGF.gets_lineno);
  • return ret;
    }

@@ -8253,4 +8272,5 @@ Init_IO(void)
id_flush = rb_intern(“flush”);
id_readpartial = rb_intern(“readpartial”);

  • id_lineno = rb_intern("$.");

    rb_define_global_function(“syscall”, rb_f_syscall, -1);
    Index: test/ruby/test_argf.rb
    ===================================================================
    — test/ruby/test_argf.rb (revision 19983)
    +++ test/ruby/test_argf.rb (working copy)
    @@ -63,13 +63,13 @@ class TestArgf < Test::Unit::TestCase
    a.rewind
    b.rewind

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["1", 1, 
    

“1”, 3]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["2", 2, 
    

“2”, 4]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["3", 3, 
    

“3”, 5]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["4", 4, 
    

“4”, 6]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, 
    

“5”, 7]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["1", 1, 
    

“1”, 1]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["2", 2, 
    

“2”, 2]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["3", 3, 
    

“3”, 3]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["4", 4, 
    

“4”, 4]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, 
    

“5”, 5]
a.rewind
b.rewind

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, 
    

“5”, 8]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["6", 6, 
    

“6”, 9]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, 
    

“5”, 5]

  •  p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["6", 6, 
    

“6”, 6]
SRC
expected = src.scan(/#=> (.+)/).flatten
@@ -91,8 +91,8 @@ class TestArgf < Test::Unit::TestCase
a.gets; p $. #=> 2
a.gets; p $. #=> 3

  •  a.rewind; p $.  #=> 3
    
  •  a.rewind; p $.  #=> 2
     a.gets; p $.  #=> 3
     a.gets; p $.  #=> 4
    
  •  a.rewind; p $.  #=> 4
    
  •  a.rewind; p $.  #=> 2
     a.gets; p $.  #=> 3
     a.lineno = 1000; p $.  #=> 1000
    

@@ -114,12 +114,12 @@ class TestArgf < Test::Unit::TestCase
a.gets; p $. #=> 1
a.gets; p $. #=> 2

  •  a.gets; p $.  #=> 1
    
  •  a.rewind; p $.  #=> 1
    
  •  a.gets; p $.  #=> 1
    
  •  a.gets; p $.  #=> 2
    
  •  a.gets; p $.  #=> 1
    
  •  a.lineno = 1000; p $.  #=> 1
    
  •  a.gets; p $.  #=> 2
    
  •  a.gets; p $.  #=> 2
    
  •  a.gets; p $.  #=> 3
    
  •  a.rewind; p $.  #=> 2
    
  •  a.gets; p $.  #=> 3
    
  •  a.gets; p $.  #=> 4
    
  •  a.gets; p $.  #=> 5
    
  •  a.lineno = 1000; p $.  #=> 1000
    
  •  a.gets; p $.  #=> 1001
    
  •  a.gets; p $.  #=> 1001
     $. = 2000
     a.gets; p $.  #=> 2000