Skip to content

Instantly share code, notes, and snippets.

@sokratis12GR
Last active August 26, 2016 20:37
Show Gist options
  • Save sokratis12GR/14a07a302929f8116ed2daa7403316ff to your computer and use it in GitHub Desktop.
Save sokratis12GR/14a07a302929f8116ed2daa7403316ff to your computer and use it in GitHub Desktop.
class String
def underscore
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
end
Dir.glob("*.{json,png}").each do |entry|
# Renaming the files while adding `_` between the words and making them lowercase
next if entry == '.' || entry == '..'
new_entry = entry.underscore
File.rename(entry, new_entry)
puts "Renamed #{entry} to #{new_entry}."
old_text = File.basename("*.json", ".json")
new_text = File.basename("*.json", ".json")
text = File.read(file_name)
new_contents = text.gsub(old_text, new_text)
# To merely print the contents of the file, use:
puts new_contents
# To write changes to the file, use:
File.open(file_name, "w") {|file| file.puts new_contents }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment