Skip to content

Instantly share code, notes, and snippets.

@tacitphoenix
Created January 15, 2014 23:51
Show Gist options
  • Save tacitphoenix/8447138 to your computer and use it in GitHub Desktop.
Save tacitphoenix/8447138 to your computer and use it in GitHub Desktop.
Simple file manipulation Ruby snippets
File.open(fname).read.match(/\"accepted_fields\":\s+\[((\d(,)?)+)\]/)
Dir.glob(fnames).each do |fname|
convertFile = File.open(fname).read.match(/\"accepted_fields\":\s+\[((\d(,)?)+)\]/)
next unless convertFile
tempfile = "#{fname}.tmp"
File.open(tempfile, 'w') do |f|
IO.foreach(fname) do |line|
if(line =~ /\"accepted_fields\":\s+\[((\d(,)?)+)\]/)
ids = line.match(/\[(.*)\]/)[1].split(",").map{|x| x.to_i}
names = ContentType.where(id: ids).pluck(:name)
f.puts line.sub(/\[.*\]/, names.to_s)
else
f.puts line
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment