I came across an issue where scattered ruby source files had non-ASCII characters. If you're asking how problems arising from this could have occurred in the first place, you're not wrong in thinking it's a WTF.
In any case, rather than tracking these offending files down one by one (which can be tedious in large projects without impeccable test coverage), I thought it would be easier to add magic encoding comments to all of the ruby source code files thus obviating the need for deep code auditing. Thanks to sed, awk, and a brother who is a master of shell scripting helping me out, I came up with this quick script to get the job done:
Run this script from the root of your project, and you'll have magic encoding comments at the top of your source files.
If you notice the regex, it is checking the first line of each file in order to avoid adding the comment to files starting with a hash bang so as to avoid breaking any scripts — I didn't have many of those in the first place. Also worth noting is that I specify /bin/zsh explicitly since I am depending on recursive file globbing (**/*.rb).
It would have been nice to also avoid any files that already had magic encoding comments, however magic encoding comment format rules are rather loose, and so I ignored that aspect entirely.