Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Bhacaz/693a20b0fdff998d85ff8477e7fa8c69 to your computer and use it in GitHub Desktop.
Save Bhacaz/693a20b0fdff998d85ff8477e7fa8c69 to your computer and use it in GitHub Desktop.
Script to add `# frozen_string_literal: true` on top of a file. Useful to add has a external tool in RubyMine
#!/usr/bin/env ruby
file_path = ARGV.first
puts ''
unless file_path
puts 'You must provide a file path'
puts ' Ex: ruby add_frozen_string.rb /path/to/file.rb'
exit 1
end
puts "Opening: #{file_path}"
f = File.open(file_path, "r+")
lines = f.readlines
f.close
if lines.first == "# frozen_string_literal: true\n"
puts 'Already there.'
exit 0
end
lines = ["# frozen_string_literal: true\n\n"] + lines
output = File.new(file_path, "w")
lines.each { |line| output.write line }
output.close
puts 'Done!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment