diff options
-rw-r--r-- | io.c | 10 | ||||
-rw-r--r-- | test/ruby/test_argf.rb | 17 |
2 files changed, 19 insertions, 8 deletions
@@ -11098,11 +11098,19 @@ argf_set_pos(VALUE argf, VALUE offset) static VALUE argf_rewind(VALUE argf) { + VALUE ret; + int old_lineno; + if (!next_argv()) { rb_raise(rb_eArgError, "no stream to rewind"); } ARGF_FORWARD(0, 0); - return rb_io_rewind(ARGF.current_file); + old_lineno = RFILE(ARGF.current_file)->fptr->lineno; + ret = rb_io_rewind(ARGF.current_file); + if (!global_argf_p(argf)) { + ARGF.last_lineno = ARGF.lineno -= old_lineno; + } + return ret; } /* diff --git a/test/ruby/test_argf.rb b/test/ruby/test_argf.rb index 5c3805ca19..fdd31c630a 100644 --- a/test/ruby/test_argf.rb +++ b/test/ruby/test_argf.rb @@ -78,15 +78,15 @@ class TestArgf < Test::Unit::TestCase p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["2", 2, "2", 2] 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 end @@ -144,6 +144,9 @@ class TestArgf < Test::Unit::TestCase assert_equal(3, f.lineno) assert_equal((1..3).map {|i| [i, "#{i}\n"]}, result) + f.rewind + assert_equal(2, f.lineno) + f = ARGF.class.new(@t1.path, @t2.path, @t3.path) f.each_char.to_a assert_equal(0, f.lineno) |