Skip to content

Instantly share code, notes, and snippets.

@marcandre
Created October 6, 2018 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcandre/c7992f788e561623fc755fbf05c922fa to your computer and use it in GitHub Desktop.
Save marcandre/c7992f788e561623fc755fbf05c922fa to your computer and use it in GitHub Desktop.
require -> require_relative
require 'pathname'
Pathname('./lib').children.select(&:directory?).map(&:basename).each do |name|
Dir["./lib/#{name}/**/*.rb"].each do |path|
begin
code = File.read(path)
code.gsub!(%r{^(\s*)require (['"])#{name}/([\w/]*)['"]}) do
relative_path = Pathname("./lib/#{name}/#{$3}.rb").relative_path_from(Pathname(path).dirname)
%Q{#{$1}require_relative #{$2}#{relative_path.to_s[0..-4]}#{$2}}
end
File.write(path, code)
rescue Exception => e
puts "Error while processing #{path}:", e
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment