Skip to content

Instantly share code, notes, and snippets.

@PrimaryFeather
Created December 5, 2015 11:51
Show Gist options
  • Save PrimaryFeather/ba8b2eca965ac6f8baad to your computer and use it in GitHub Desktop.
Save PrimaryFeather/ba8b2eca965ac6f8baad to your computer and use it in GitHub Desktop.
A small Ruby script that modifies source code so that variables in the form "mName" are changed to "_name"
#!/usr/bin/ruby
if ARGV.count < 1
puts "Usage: #{script_name} source_path"
puts " (e.g. ../src/starling/)"
exit
end
search_path = ARGV[0]
Dir[search_path + "**/*.as"].each do |source_file|
puts "converting " + source_file
lines = []
File.open(source_file, "r") do |file|
file.each do |line|
line_changes = false
while m = line.match(/(\W)(m[A-Z]\w*)/) do
line_changes = true
old_word = m[2]
new_word =
if old_word == "mID" then "_id"
else '_' + old_word[1].downcase + old_word[2..-1] end
line[m.begin(2)...m.end(2)] = new_word
end
line = line.rstrip + "\n" if line_changes
lines << line
end
end
File.open(source_file, "w") do |file|
file << lines.join
end
end
@PrimaryFeather
Copy link
Author

This code was used to modify all of Starling's member variables from the "m"-prefix to the more widely used underscore ("_").
Note that the file ending (".as") is hard-coded in this script. If you want to use it for different file types, modify line 11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment