From 612aa5c24a7c249867bbcd7d6567012aa6a7f4b9 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 21 Nov 2022 16:20:17 +0900 Subject: [PATCH] Use class methods of `File` over `Kernel.open` and `IO.read` --- enc/make_encmake.rb | 2 +- ext/extmk.rb | 8 +++--- ext/socket/extconf.rb | 4 +-- lib/mkmf.rb | 2 +- tool/checksum.rb | 4 +-- tool/enc-case-folding.rb | 4 +-- tool/enc-emoji-citrus-gen.rb | 4 +-- tool/expand-config.rb | 2 +- tool/gen-mailmap.rb | 2 +- tool/make_hgraph.rb | 7 +++--- tool/rbinstall.rb | 47 +++++++++++++++++------------------- win32/mkexports.rb | 2 +- 12 files changed, 39 insertions(+), 49 deletions(-) diff --git a/enc/make_encmake.rb b/enc/make_encmake.rb index bc0597e3f4..fcfc2c9267 100755 --- a/enc/make_encmake.rb +++ b/enc/make_encmake.rb @@ -134,7 +134,7 @@ else end mkin = File.read(File.join($srcdir, "Makefile.in")) mkin.gsub!(/@(#{CONFIG.keys.join('|')})@/) {CONFIG[$1]} -open(ARGV[0], 'wb') {|f| +File.open(ARGV[0], 'wb') {|f| f.puts mkin, dep } if MODULE_TYPE == :static diff --git a/ext/extmk.rb b/ext/extmk.rb index cc560be1f2..cab9a519c1 100755 --- a/ext/extmk.rb +++ b/ext/extmk.rb @@ -144,7 +144,7 @@ def extmake(target, basedir = 'ext', maybestatic = true) d = target until (d = File.dirname(d)) == '.' if File.exist?("#{$top_srcdir}/#{basedir}/#{d}/extconf.rb") - parent = (/^all:\s*install/ =~ IO.read("#{d}/Makefile") rescue false) + parent = (/^all:\s*install/ =~ File.read("#{d}/Makefile") rescue false) break end end @@ -447,9 +447,8 @@ if $extstatic end for dir in ["ext", File::join($top_srcdir, "ext")] setup = File::join(dir, CONFIG['setup']) - if File.file? setup - f = open(setup) - while line = f.gets() + if (f = File.stat(setup) and f.file? rescue next) + File.foreach(setup) do |line| line.chomp! line.sub!(/#.*$/, '') next if /^\s*$/ =~ line @@ -466,7 +465,6 @@ for dir in ["ext", File::join($top_srcdir, "ext")] end MTIMES << f.mtime $setup = setup - f.close break end end unless $extstatic diff --git a/ext/socket/extconf.rb b/ext/socket/extconf.rb index 73bbc8e687..37ff216560 100644 --- a/ext/socket/extconf.rb +++ b/ext/socket/extconf.rb @@ -552,7 +552,7 @@ EOS end if !have_macro("IPPROTO_IPV6", headers) && have_const("IPPROTO_IPV6", headers) - IO.read(File.join(File.dirname(__FILE__), "mkconstants.rb")).sub(/\A.*^__END__$/m, '').split(/\r?\n/).grep(/\AIPPROTO_\w*/){$&}.each {|name| + File.read(File.join(File.dirname(__FILE__), "mkconstants.rb")).sub(/\A.*^__END__$/m, '').split(/\r?\n/).grep(/\AIPPROTO_\w*/){$&}.each {|name| have_const(name, headers) unless $defs.include?("-DHAVE_CONST_#{name.upcase}") } end @@ -679,7 +679,7 @@ SRC end end FileUtils.mkdir_p(File.dirname(hdr)) - open(hdr, "w") {|f| f.write(in6)} + File.write(hdr, in6) $distcleanfiles << hdr $distcleandirs << File.dirname(hdr) "done" diff --git a/lib/mkmf.rb b/lib/mkmf.rb index 3d0beebbf2..fba1e678c7 100644 --- a/lib/mkmf.rb +++ b/lib/mkmf.rb @@ -1762,7 +1762,7 @@ SRC hdr << "#endif\n" hdr = hdr.join("") log_src(hdr, "#{header} is") - unless (IO.read(header) == hdr rescue false) + unless (File.read(header) == hdr rescue false) File.open(header, "wb") do |hfile| hfile.write(hdr) end diff --git a/tool/checksum.rb b/tool/checksum.rb index bcc60ee14a..8f2d1d97d0 100755 --- a/tool/checksum.rb +++ b/tool/checksum.rb @@ -36,9 +36,7 @@ class Checksum end def update! - open(@checksum, "wb") {|f| - f.puts("src=\"#{@source}\", len=#{@len}, checksum=#{@sum}") - } + File.binwrite(@checksum, "src=\"#{@source}\", len=#{@len}, checksum=#{@sum}") end def update diff --git a/tool/enc-case-folding.rb b/tool/enc-case-folding.rb index 6d43a27df8..76c6b5c48b 100755 --- a/tool/enc-case-folding.rb +++ b/tool/enc-case-folding.rb @@ -409,9 +409,7 @@ if $0 == __FILE__ s = f.string end if dest - open(dest, "wb") do |file| - file.print(s) - end + File.binwrite(dest, s) else STDOUT.print(s) end diff --git a/tool/enc-emoji-citrus-gen.rb b/tool/enc-emoji-citrus-gen.rb index da9c8a6b62..0b37e48d3f 100644 --- a/tool/enc-emoji-citrus-gen.rb +++ b/tool/enc-emoji-citrus-gen.rb @@ -71,7 +71,7 @@ end def generate_to_ucs(params, pairs) pairs.sort_by! {|u, c| c } name = "EMOJI_#{params[:name]}%UCS" - open("#{name}.src", "w") do |io| + File.open("#{name}.src", "w") do |io| io.print header(params.merge(name: name.tr('%', '/'))) io.puts io.puts "BEGIN_MAP" @@ -83,7 +83,7 @@ end def generate_from_ucs(params, pairs) pairs.sort_by! {|u, c| u } name = "UCS%EMOJI_#{params[:name]}" - open("#{name}.src", "w") do |io| + File.open("#{name}.src", "w") do |io| io.print header(params.merge(name: name.tr('%', '/'))) io.puts io.puts "BEGIN_MAP" diff --git a/tool/expand-config.rb b/tool/expand-config.rb index 81ffa6cb98..928e7f91fe 100755 --- a/tool/expand-config.rb +++ b/tool/expand-config.rb @@ -16,7 +16,7 @@ while /\A(\w+)=(.*)/ =~ ARGV[0] end if $output - output = open($output, "wb", $mode &&= $mode.oct) + output = File.open($output, "wb", $mode &&= $mode.oct) output.chmod($mode) if $mode else output = STDOUT diff --git a/tool/gen-mailmap.rb b/tool/gen-mailmap.rb index df1884520c..0cdedf1e7b 100755 --- a/tool/gen-mailmap.rb +++ b/tool/gen-mailmap.rb @@ -13,7 +13,7 @@ YAML.load(DATA.read).each do |name, mails| email[name] |= mails end -open(File.join(__dir__, "../.mailmap"), "w") do |f| +File.open(File.join(__dir__, "../.mailmap"), "w") do |f| email.each do |name, mails| canonical = "#{ name }@ruby-lang.org" mails.delete(canonical) diff --git a/tool/make_hgraph.rb b/tool/make_hgraph.rb index 0f388814dd..174fa5dd2f 100644 --- a/tool/make_hgraph.rb +++ b/tool/make_hgraph.rb @@ -83,13 +83,12 @@ module ObjectSpace def self.module_refenreces_image klass, file dot = module_refenreces_dot(klass) - img = nil - IO.popen("dot -Tpng", 'r+'){|io| + img = IO.popen(%W"dot -Tpng", 'r+b') {|io| # io.puts dot io.close_write - img = io.read + io.read } - open(File.expand_path(file), 'w+'){|f| f.puts img} + File.binwrite(file, img) end end diff --git a/tool/rbinstall.rb b/tool/rbinstall.rb index e94445114b..02ce7faaa7 100755 --- a/tool/rbinstall.rb +++ b/tool/rbinstall.rb @@ -143,7 +143,7 @@ def parse_args(argv = ARGV) if $installed_list ||= $mflags.defined?('INSTALLED_LIST') RbConfig.expand($installed_list, RbConfig::CONFIG) - $installed_list = open($installed_list, "ab") + $installed_list = File.open($installed_list, "ab") $installed_list.sync = true end @@ -291,11 +291,11 @@ def install_recursive(srcdir, dest, options = {}) end def open_for_install(path, mode) - data = open(realpath = with_destdir(path), "rb") {|f| f.read} rescue nil + data = File.binread(realpath = with_destdir(path)) rescue nil newdata = yield unless $dryrun unless newdata == data - open(realpath, "wb", mode) {|f| f.write newdata} + File.open(realpath, "wb", mode) {|f| f.write newdata} end File.chmod(mode, realpath) end @@ -550,7 +550,7 @@ $script_installer = Class.new(installer) do def install(src, cmd) cmd = cmd.sub(/[^\/]*\z/m) {|n| transform(n)} - shebang, body = open(src, "rb") do |f| + shebang, body = File.open(src, "rb") do |f| next f.gets, f.read end shebang or raise "empty file - #{src}" @@ -622,7 +622,7 @@ install?(:local, :comm, :man) do has_goruby = File.exist?(goruby_install_name+exeext) require File.join(srcdir, "tool/mdoc2man.rb") if /\Adoc\b/ !~ $mantype mdocs.each do |mdoc| - next unless File.file?(mdoc) and open(mdoc){|fh| fh.read(1) == '.'} + next unless File.file?(mdoc) and File.read(mdoc, 1) == '.' base = File.basename(mdoc) if base == "goruby.1" next unless has_goruby @@ -634,17 +634,14 @@ install?(:local, :comm, :man) do if /\Adoc\b/ =~ $mantype if compress - w = open(mdoc) {|f| - stdin = STDIN.dup - STDIN.reopen(f) - begin - destfile << suffix - IO.popen(compress, &:read) - ensure - STDIN.reopen(stdin) - stdin.close - end - } + begin + w = IO.popen(compress, "rb", in: mdoc, &:read) + rescue + else + destfile << suffix + end + end + if w open_for_install(destfile, $data_mode) {w} else install mdoc, destfile, :mode => $data_mode @@ -657,19 +654,19 @@ install?(:local, :comm, :man) do File.basename(mdoc).start_with?('gemfile') w = File.read(mdoc) else - open(mdoc) {|r| Mdoc2Man.mdoc2man(r, w)} + File.open(mdoc) {|r| Mdoc2Man.mdoc2man(r, w)} w = w.join("") end if compress - require 'tmpdir' - Dir.mktmpdir("man") {|d| - dest = File.join(d, File.basename(destfile)) - File.open(dest, "wb") {|f| f.write w} - if system(compress, dest) - w = File.open(dest+suffix, "rb") {|f| f.read} - destfile << suffix + begin + w = IO.popen(compress, "r+b") do |f| + Thread.start {f.write w; f.close_write} + f.read end - } + rescue + else + destfile << suffix + end end open_for_install(destfile, $data_mode) {w} end diff --git a/win32/mkexports.rb b/win32/mkexports.rb index 6808a38ea0..40f055dee7 100755 --- a/win32/mkexports.rb +++ b/win32/mkexports.rb @@ -26,7 +26,7 @@ class Exports def self.output(output = $output, &block) if output - open(output, 'wb', &block) + File.open(output, 'wb', &block) else yield STDOUT end