Project

General

Profile

Actions

Bug #171

closed

StringIO#each and StringIO#readlines does change $_, IO#each and IO#readlines do not

Added by NoKarma (Arthur Schreiber) over 16 years ago. Updated over 13 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
[ruby-core:17277]

Description

=begin
Changeset r17352 has fixed the issue reported in #159, but has introduced two new ones:

First: StringIO#readlines was changed to set $_ to nil:

$_ = "test"
File.open("test.file") { |f| f.readlines } # => ["line 1\n", "line 2\n", "line 3"]
$_ # => "test"

require "stringio"
$_ = "test"
StringIO.new("line 1\nline 2\nline 3").readlines # => ["line 1\n", "line 2\n", "line 3"]
$_ # => nil

Second: StringIO#each was changed to change $_ on each yield:

$_ = "test"
File.open("test.file") do |f|
f.each do |l|
# >> Line: "line 1\n" - "test"
# >> Line: "line 2\n" - "test"
# >> Line: "line 3" - "test"
puts "Line: #{l.inspect} - #{$.inspect}"
end
end
$
# => "test"

require "stringio"
$_ = "test"
strio = StringIO.new("line 1\nline 2\nline 3")
strio.each do |l|
# >> Line: "line 1\n" - "line 1\n"
# >> Line: "line 2\n" - "line 2\n"
# >> Line: "line 3" - "line 3"
puts "Line: #{l.inspect} - #{$.inspect}"
end
$
# => "line 3"
=end

Actions

Also available in: Atom PDF

Like0
Like0