m17n in Standalone Scripts
Ruby is a scripting language at heart. Although the earlier m17n represents an elegant and highly flexible system, it would be tedious to think about all of its details when you just want to process some datafiles or run a quick one-liner on the command line. Although we won’t spend much time going over the minute details, I want to make sure that you get a chance to see how Ruby lets the m17n system get out of your way a bit when it comes to day-to-day scripting needs.
Inferring Encodings from Locale
The key things that Ruby’s m17n system can modify when it comes to encodings are the source encoding, the default external encoding of files, and the default internal encoding that loaded files should be transcoded to. The following simple script inspects each of these values to see how they relate to your system’s locale:
puts "Source encoding: #{__ENCODING__.inspect}" puts "Default external: #{Encoding.default_external.inspect}" puts "Default internal: #{Encoding.default_internal.inspect}" puts "Locale charmap: #{ Encoding.locale_charmap.inspect}" puts "LANG environment variable: #{ENV['LANG'].inspect}"
When we run this code without a magic comment, I get the following output on my system. Your exact values may vary, as I’ll explain in a moment:
$ ruby encoding_checker.rb Source encoding: #<Encoding:US-ASCII> Default external: #<Encoding:UTF-8> Default internal: nil Locale charmap: "UTF-8" LANG environment variable: "en_US.UTF-8"
Here, we see that the source ...
Get Ruby Best Practices now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.