I have seen several ways of specifying the string encoding as follows:
# -*- coding: utf-8 -*-# coding: utf-8# encoding: utf-8#!/usr/bin/env ruby -Ku#!/usr/bin/env ruby -Eutf-8Encoding.default_external = "utf-8"
Are there any other? Can someone tell me their difference if any, and their origin if there are any? Are there old ones and new ones; minor ones and popular ones; depreciated ones and appreciated ones?
TL;DR version: use
# coding: utf-8or# encoding: utf-8; they are modern and there is no difference between them.According to this most enlightening article In Ruby 1.9 the rule is that the magic comment must be:
So that covers 1, 2 & 3 and probably would include stuff like
# foobarcoding: utf-8as well. This is the preferred method for Ruby 1.9.For compatibility reasons there are retained the hash bang
-K*switches from Ruby 1.8, which covers 4.Numbers five and six cover a slightly different thing. I recommend reading through the aforelinked article to see how external and internal encodings work exactly. However the gist is that when you read data via an IO object it matters how that data is encoded for reading it correctly. External encoding expresses just that. So when you set external encoding to UTF-8 you imply that the file you are reading is encoded in UTF-8. The internal encoding is to what encoding should Ruby automatically transcode the resulting string from that operation.
The default you are setting is used when an external encoding is not set explicitly. These defaults can be changed via the
-Eflag in the hash bang (number 5; thus 5 and six would work identically).Passing
-Uwill set the internal encoding to UTF-8 (meaning that strings will be transcoded automatically to UTF-8 when read).