Skip to content

Instantly share code, notes, and snippets.

@nas
Created November 16, 2012 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nas/4089210 to your computer and use it in GitHub Desktop.
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
#!/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