Skip to content

Instantly share code, notes, and snippets.

@ken47
Created September 16, 2012 19:46
Show Gist options
  • Save ken47/3734083 to your computer and use it in GitHub Desktop.
Save ken47/3734083 to your computer and use it in GitHub Desktop.
string parsing w ruby
text = File.read(filepath)
if text.include? "console.log"
puts '########'
puts "console.log statements detected in " + filepath.split('/').last
puts '########'
next_console_log = 0
while true do
next_console_log = text.index('console.log', next_console_log)
if next_console_log.nil?
break
end
next_opening_paren = text.index('(',next_console_log)
next_closing_paren = text.index(')',next_console_log)
# once this loop is broken, next_closing_paren should contain the position of the closing paren for console.log
while true do
next_opening_paren = text.index('(',next_opening_paren+1)
if next_opening_paren.nil?
break
end
if next_opening_paren > next_closing_paren
break
else
next_closing_paren = text.index(')',next_closing_paren+1)
end
end
puts text[next_console_log..next_closing_paren]
text.insert (next_closing_paren +1 ), "*/"
text.insert (next_console_log), "/*"
next_console_log += 3
puts text[(next_console_log-3)..(next_closing_paren+4)]
end
end
File.open(@@tmp, 'w') {|file| file.write text}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment