Created
January 15, 2014 23:51
-
-
Save tacitphoenix/8447138 to your computer and use it in GitHub Desktop.
Simple file manipulation Ruby snippets
This file contains 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
File.open(fname).read.match(/\"accepted_fields\":\s+\[((\d(,)?)+)\]/) |
This file contains 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
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