Created
November 16, 2012 17:28
-
-
Save nas/4089210 to your computer and use it in GitHub Desktop.
Simple script to replace lambda to proc in all files within a directory for rails named_scopes. Can be used for any other text replacement by just modifying the regex and substitution code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
directory = "app/models" | |
all_files = Dir.glob(File.join('app/models', '**', '*.rb')) | |
puts "Potentially modifying #{all_files.size}. Do you want to continue? [y/n]" | |
reply = gets | |
exit unless %w(y yes).include? reply.chomp.downcase | |
all_files.each do |filename| | |
file_modified = false | |
lines = IO.readlines(filename) | |
lines.each_with_index do |line, i| | |
r = Regexp.new /named_scope\s+:(.*),\s+(lambda)/ | |
if r.match(line) | |
lines[i] = line.sub('lambda', 'proc') | |
puts "Old line:\n#{line.strip}\n----------" | |
puts "New line:\n#{lines[i].strip}\n----------" | |
file_modified = true | |
end | |
end | |
puts "Updated #{filename}\n\n" if file_modified | |
File.open(filename, 'w') {|f| f.puts lines} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment