rod mclaughlin


How to get rid of all those pesky hashrockets => (09 aug 19)

#!/usr/bin/env ruby

files = Dir.glob('app/**/*.rb') + Dir.glob('app/**/*.haml') + Dir.glob('app/**/*.erb')
total = 0
changed = 0
files.each do |file|
  changeable = false
  str = File.read( file )
  new_text = ""
  str.lines.each do |t|
    total += 1
    u = t
    if u.include? '=>'
      u = u.gsub(/([:])([A-Za-z1-9]+)([A-Za-z1-9 ]+)([=>])/, "\\2:\\3").gsub( " >", "" )
    end
    unless t == u
      changeable = true
      changed += 1
    end
    new_text += u
  end
  if changeable && (ARGV.include? '-r')
    File.open(file, 'w') { |file| file.write(new_text) }
  else
    puts new_text
  end
end
puts "Total number of files #{ files.size }, total lines #{ total }, #{ changed } lines to change"
puts "Run with '-r' as the first parameter to change files" unless ARGV.include? '-r'



Back
Portland London