Project

General

Profile

Bug #171

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

Added by NoKarma (Arthur Schreiber) about 12 years ago. Updated over 9 years ago.

Status:
Closed
Priority:
Normal
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

#1

Updated by nobu (Nobuyoshi Nakada) about 12 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

=begin
Applied in changeset r17366.
=end

Also available in: Atom PDF