Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion enc/make_encmake.rb
Expand Up @@ -134,7 +134,7 @@ def target_transcoders
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
Expand Down
8 changes: 3 additions & 5 deletions ext/extmk.rb
Expand Up @@ -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
Expand Down Expand Up @@ -447,9 +447,8 @@ def $mflags.defined?(var)
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
Expand All @@ -466,7 +465,6 @@ def $mflags.defined?(var)
end
MTIMES << f.mtime
$setup = setup
f.close
break
end
end unless $extstatic
Expand Down
4 changes: 2 additions & 2 deletions ext/socket/extconf.rb
Expand Up @@ -552,7 +552,7 @@ def %(s) s || self end
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
Expand Down Expand Up @@ -679,7 +679,7 @@ def %(s) s || self end
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"
Expand Down
2 changes: 1 addition & 1 deletion lib/mkmf.rb
Expand Up @@ -1762,7 +1762,7 @@ def create_header(header = "extconf.h")
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
Expand Down
4 changes: 1 addition & 3 deletions tool/checksum.rb
Expand Up @@ -36,9 +36,7 @@ def update?
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
Expand Down
4 changes: 1 addition & 3 deletions tool/enc-case-folding.rb
Expand Up @@ -409,9 +409,7 @@ def debug!() end
s = f.string
end
if dest
open(dest, "wb") do |file|
file.print(s)
end
File.binwrite(dest, s)
else
STDOUT.print(s)
end
Expand Down
4 changes: 2 additions & 2 deletions tool/enc-emoji-citrus-gen.rb
Expand Up @@ -71,7 +71,7 @@ def header(params)
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"
Expand All @@ -83,7 +83,7 @@ def generate_to_ucs(params, pairs)
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"
Expand Down
2 changes: 1 addition & 1 deletion tool/expand-config.rb
Expand Up @@ -16,7 +16,7 @@
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
Expand Down
2 changes: 1 addition & 1 deletion tool/gen-mailmap.rb
Expand Up @@ -13,7 +13,7 @@
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)
Expand Down
7 changes: 3 additions & 4 deletions tool/make_hgraph.rb
Expand Up @@ -83,13 +83,12 @@ def self.module_refenreces_dot klass

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
47 changes: 22 additions & 25 deletions tool/rbinstall.rb
Expand Up @@ -143,7 +143,7 @@ def $mflags.defined?(var)

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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -550,7 +550,7 @@ def prolog(shebang)
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}"
Expand Down Expand Up @@ -622,7 +622,7 @@ def stub
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
Expand All @@ -634,17 +634,14 @@ def stub

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
Expand All @@ -657,19 +654,19 @@ class << (w = [])
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
Expand Down
2 changes: 1 addition & 1 deletion win32/mkexports.rb
Expand Up @@ -26,7 +26,7 @@ def self.extract(objs, *rest)

def self.output(output = $output, &block)
if output
open(output, 'wb', &block)
File.open(output, 'wb', &block)
else
yield STDOUT
end
Expand Down