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
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

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

@ -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

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

@ -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"

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

@ -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

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

@ -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

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

@ -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

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

@ -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"

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

@ -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

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

@ -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)

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

@ -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

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

@ -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

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

@ -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