Use class methods of `File` over `Kernel.open` and `IO.read`

This commit is contained in:
Nobuyoshi Nakada 2022-11-21 16:20:17 +09:00
Родитель 116920cff8
Коммит 612aa5c24a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 7CD2805BFA3770C6
12 изменённых файлов: 39 добавлений и 49 удалений

Просмотреть файл

@ -134,7 +134,7 @@ else
end end
mkin = File.read(File.join($srcdir, "Makefile.in")) mkin = File.read(File.join($srcdir, "Makefile.in"))
mkin.gsub!(/@(#{CONFIG.keys.join('|')})@/) {CONFIG[$1]} mkin.gsub!(/@(#{CONFIG.keys.join('|')})@/) {CONFIG[$1]}
open(ARGV[0], 'wb') {|f| File.open(ARGV[0], 'wb') {|f|
f.puts mkin, dep f.puts mkin, dep
} }
if MODULE_TYPE == :static if MODULE_TYPE == :static

Просмотреть файл

@ -144,7 +144,7 @@ def extmake(target, basedir = 'ext', maybestatic = true)
d = target d = target
until (d = File.dirname(d)) == '.' until (d = File.dirname(d)) == '.'
if File.exist?("#{$top_srcdir}/#{basedir}/#{d}/extconf.rb") 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 break
end end
end end
@ -447,9 +447,8 @@ if $extstatic
end end
for dir in ["ext", File::join($top_srcdir, "ext")] for dir in ["ext", File::join($top_srcdir, "ext")]
setup = File::join(dir, CONFIG['setup']) setup = File::join(dir, CONFIG['setup'])
if File.file? setup if (f = File.stat(setup) and f.file? rescue next)
f = open(setup) File.foreach(setup) do |line|
while line = f.gets()
line.chomp! line.chomp!
line.sub!(/#.*$/, '') line.sub!(/#.*$/, '')
next if /^\s*$/ =~ line next if /^\s*$/ =~ line
@ -466,7 +465,6 @@ for dir in ["ext", File::join($top_srcdir, "ext")]
end end
MTIMES << f.mtime MTIMES << f.mtime
$setup = setup $setup = setup
f.close
break break
end end
end unless $extstatic end unless $extstatic

Просмотреть файл

@ -552,7 +552,7 @@ EOS
end end
if !have_macro("IPPROTO_IPV6", headers) && have_const("IPPROTO_IPV6", headers) 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}") have_const(name, headers) unless $defs.include?("-DHAVE_CONST_#{name.upcase}")
} }
end end
@ -679,7 +679,7 @@ SRC
end end
end end
FileUtils.mkdir_p(File.dirname(hdr)) FileUtils.mkdir_p(File.dirname(hdr))
open(hdr, "w") {|f| f.write(in6)} File.write(hdr, in6)
$distcleanfiles << hdr $distcleanfiles << hdr
$distcleandirs << File.dirname(hdr) $distcleandirs << File.dirname(hdr)
"done" "done"

Просмотреть файл

@ -1762,7 +1762,7 @@ SRC
hdr << "#endif\n" hdr << "#endif\n"
hdr = hdr.join("") hdr = hdr.join("")
log_src(hdr, "#{header} is") 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| File.open(header, "wb") do |hfile|
hfile.write(hdr) hfile.write(hdr)
end end

Просмотреть файл

@ -36,9 +36,7 @@ class Checksum
end end
def update! def update!
open(@checksum, "wb") {|f| File.binwrite(@checksum, "src=\"#{@source}\", len=#{@len}, checksum=#{@sum}")
f.puts("src=\"#{@source}\", len=#{@len}, checksum=#{@sum}")
}
end end
def update def update

Просмотреть файл

@ -409,9 +409,7 @@ if $0 == __FILE__
s = f.string s = f.string
end end
if dest if dest
open(dest, "wb") do |file| File.binwrite(dest, s)
file.print(s)
end
else else
STDOUT.print(s) STDOUT.print(s)
end end

Просмотреть файл

@ -71,7 +71,7 @@ end
def generate_to_ucs(params, pairs) def generate_to_ucs(params, pairs)
pairs.sort_by! {|u, c| c } pairs.sort_by! {|u, c| c }
name = "EMOJI_#{params[:name]}%UCS" 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.print header(params.merge(name: name.tr('%', '/')))
io.puts io.puts
io.puts "BEGIN_MAP" io.puts "BEGIN_MAP"
@ -83,7 +83,7 @@ end
def generate_from_ucs(params, pairs) def generate_from_ucs(params, pairs)
pairs.sort_by! {|u, c| u } pairs.sort_by! {|u, c| u }
name = "UCS%EMOJI_#{params[:name]}" 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.print header(params.merge(name: name.tr('%', '/')))
io.puts io.puts
io.puts "BEGIN_MAP" io.puts "BEGIN_MAP"

Просмотреть файл

@ -16,7 +16,7 @@ while /\A(\w+)=(.*)/ =~ ARGV[0]
end end
if $output if $output
output = open($output, "wb", $mode &&= $mode.oct) output = File.open($output, "wb", $mode &&= $mode.oct)
output.chmod($mode) if $mode output.chmod($mode) if $mode
else else
output = STDOUT output = STDOUT

Просмотреть файл

@ -13,7 +13,7 @@ YAML.load(DATA.read).each do |name, mails|
email[name] |= mails email[name] |= mails
end end
open(File.join(__dir__, "../.mailmap"), "w") do |f| File.open(File.join(__dir__, "../.mailmap"), "w") do |f|
email.each do |name, mails| email.each do |name, mails|
canonical = "#{ name }@ruby-lang.org" canonical = "#{ name }@ruby-lang.org"
mails.delete(canonical) mails.delete(canonical)

Просмотреть файл

@ -83,13 +83,12 @@ module ObjectSpace
def self.module_refenreces_image klass, file def self.module_refenreces_image klass, file
dot = module_refenreces_dot(klass) dot = module_refenreces_dot(klass)
img = nil img = IO.popen(%W"dot -Tpng", 'r+b') {|io|
IO.popen("dot -Tpng", 'r+'){|io|
# #
io.puts dot io.puts dot
io.close_write io.close_write
img = io.read io.read
} }
open(File.expand_path(file), 'w+'){|f| f.puts img} File.binwrite(file, img)
end end
end end

Просмотреть файл

@ -143,7 +143,7 @@ def parse_args(argv = ARGV)
if $installed_list ||= $mflags.defined?('INSTALLED_LIST') if $installed_list ||= $mflags.defined?('INSTALLED_LIST')
RbConfig.expand($installed_list, RbConfig::CONFIG) RbConfig.expand($installed_list, RbConfig::CONFIG)
$installed_list = open($installed_list, "ab") $installed_list = File.open($installed_list, "ab")
$installed_list.sync = true $installed_list.sync = true
end end
@ -291,11 +291,11 @@ def install_recursive(srcdir, dest, options = {})
end end
def open_for_install(path, mode) 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 newdata = yield
unless $dryrun unless $dryrun
unless newdata == data unless newdata == data
open(realpath, "wb", mode) {|f| f.write newdata} File.open(realpath, "wb", mode) {|f| f.write newdata}
end end
File.chmod(mode, realpath) File.chmod(mode, realpath)
end end
@ -550,7 +550,7 @@ $script_installer = Class.new(installer) do
def install(src, cmd) def install(src, cmd)
cmd = cmd.sub(/[^\/]*\z/m) {|n| transform(n)} 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 next f.gets, f.read
end end
shebang or raise "empty file - #{src}" shebang or raise "empty file - #{src}"
@ -622,7 +622,7 @@ install?(:local, :comm, :man) do
has_goruby = File.exist?(goruby_install_name+exeext) has_goruby = File.exist?(goruby_install_name+exeext)
require File.join(srcdir, "tool/mdoc2man.rb") if /\Adoc\b/ !~ $mantype require File.join(srcdir, "tool/mdoc2man.rb") if /\Adoc\b/ !~ $mantype
mdocs.each do |mdoc| 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) base = File.basename(mdoc)
if base == "goruby.1" if base == "goruby.1"
next unless has_goruby next unless has_goruby
@ -634,17 +634,14 @@ install?(:local, :comm, :man) do
if /\Adoc\b/ =~ $mantype if /\Adoc\b/ =~ $mantype
if compress if compress
w = open(mdoc) {|f|
stdin = STDIN.dup
STDIN.reopen(f)
begin begin
w = IO.popen(compress, "rb", in: mdoc, &:read)
rescue
else
destfile << suffix destfile << suffix
IO.popen(compress, &:read)
ensure
STDIN.reopen(stdin)
stdin.close
end end
} end
if w
open_for_install(destfile, $data_mode) {w} open_for_install(destfile, $data_mode) {w}
else else
install mdoc, destfile, :mode => $data_mode install mdoc, destfile, :mode => $data_mode
@ -657,19 +654,19 @@ install?(:local, :comm, :man) do
File.basename(mdoc).start_with?('gemfile') File.basename(mdoc).start_with?('gemfile')
w = File.read(mdoc) w = File.read(mdoc)
else else
open(mdoc) {|r| Mdoc2Man.mdoc2man(r, w)} File.open(mdoc) {|r| Mdoc2Man.mdoc2man(r, w)}
w = w.join("") w = w.join("")
end end
if compress if compress
require 'tmpdir' begin
Dir.mktmpdir("man") {|d| w = IO.popen(compress, "r+b") do |f|
dest = File.join(d, File.basename(destfile)) Thread.start {f.write w; f.close_write}
File.open(dest, "wb") {|f| f.write w} f.read
if system(compress, dest) end
w = File.open(dest+suffix, "rb") {|f| f.read} rescue
else
destfile << suffix destfile << suffix
end end
}
end end
open_for_install(destfile, $data_mode) {w} open_for_install(destfile, $data_mode) {w}
end end

Просмотреть файл

@ -26,7 +26,7 @@ class Exports
def self.output(output = $output, &block) def self.output(output = $output, &block)
if output if output
open(output, 'wb', &block) File.open(output, 'wb', &block)
else else
yield STDOUT yield STDOUT
end end