Skip to content

Commit 199ad95

Browse files
committed
[GR-18163] Update Dir.foreach to accept an encoding parameter
PullRequest: truffleruby/3235
2 parents 4c6e74e + b4f8543 commit 199ad95

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Compatibility:
4444
* Update `String#split` to raise `TypeError` when false is given (#2606, @bjfish).
4545
* Update `String#lstrip!` to remove leading null characters (#2607, @bjfish).
4646
* Update `File.utime` to return the number of file names in the arguments (#2616, @bjfish).
47+
* Update `Dir.foreach` to accept an `encoding` parameter (#2627, @bjfish).
4748

4849
Performance:
4950

spec/ruby/core/dir/foreach_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
it "accepts an encoding keyword for the encoding of the entries" do
4343
dirs = Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: "utf-8").to_a.sort
4444
dirs.each {|dir| dir.encoding.should == Encoding::UTF_8}
45+
46+
dirs = Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: Encoding::UTF_16LE).to_a.sort
47+
dirs.each {|dir| dir.encoding.should == Encoding::UTF_16LE}
48+
49+
Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: Encoding::UTF_16LE) do |f|
50+
f.encoding.should == Encoding::UTF_16LE
51+
end
4552
end
4653

4754
ruby_version_is ""..."2.7" do

spec/tags/core/dir/foreach_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main/ruby/truffleruby/core/dir.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ def glob(pattern, flags=0, base: nil, sort: true, &block)
265265
end
266266
end
267267

268-
def foreach(path)
269-
return to_enum(:foreach, path) unless block_given?
268+
def foreach(path, **options)
269+
return to_enum(:foreach, path, **options) unless block_given?
270270

271-
open(path) do |dir|
271+
open(path, **options) do |dir|
272272
while s = dir.read
273273
yield s
274274
end

0 commit comments

Comments
 (0)