Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created December 15, 2016 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/373010d9e753285046ec75e906642c61 to your computer and use it in GitHub Desktop.
Save JoshCheek/373010d9e753285046ec75e906642c61 to your computer and use it in GitHub Desktop.
Encoding exploration
forEachEncoding() {
code="$1"

echo "$code"

echo -n '  LANG=                      | '
LANG= ruby -e "p $code"

echo -n '  LANG=en_US.IBM437          | '
LANG=en_US.IBM437 ruby -e "p $code"

echo -n '  LANG=de_CH.IBM437          | '
LANG=de_CH.IBM437 ruby -e "p $code"

echo -n '  # encoding: IBM437         | '
ruby -e "# encoding: IBM437
p $code"

echo -n '  --internal-encoding IBM437 | '
ruby --internal-encoding IBM437 -e "p $code"

echo -n '  --external-encoding IBM437 | '
ruby --external-encoding IBM437 -e "p $code"

echo -n '  -Ks                        | '
ruby -Ks -e "p $code"
}


forEachEncoding 'Encoding.default_internal'
forEachEncoding 'Encoding.default_external'
forEachEncoding 'Encoding.locale_charmap'
forEachEncoding 'Encoding.find("locale")'
forEachEncoding 'Encoding.find("external")'
forEachEncoding 'Encoding.find("internal")'
forEachEncoding 'Encoding.find("locale")'
forEachEncoding 'Encoding.find("filesystem")'
forEachEncoding 'Encoding.find("filesystem")'

forEachEncoding '$stdin.internal_encoding'
forEachEncoding '$stdin.external_encoding'
forEachEncoding '$stdout.internal_encoding'
forEachEncoding '$stdout.external_encoding'

forEachEncoding 'IO.pipe.first.internal_encoding'
forEachEncoding 'IO.pipe.first.external_encoding'
forEachEncoding 'IO.pipe.last.internal_encoding'
forEachEncoding 'IO.pipe.last.external_encoding'

forEachEncoding '__ENCODING__'
forEachEncoding '"".encoding'

I'm piping it through this monstrosity to try and make sense of all the info:

ruby -rpp -e 'rows_of_cols = $stdin.read.split(/^(?=\S)/).map { |row| row_name, *cols = row.lines.map(&:chomp); [row_name, cols.map { |col| col.split("|").map(&:strip) }] }; colnames = rows_of_cols.first.last.map(&:first); rownames = rows_of_cols.map(&:first); rows = rows_of_cols.map(&:last).map { |cols| cols.map(&:last) }; format = "%-#{rownames.max_by(&:length).length}s | #{[colnames, *rows].transpose.map { |cs| "%-#{cs.max_by(&:length).length}s" }.join(" | ")}\n"; printf format, "", *colnames; printf format, *format.scan(/\d+/).map { |n| "-" * n.to_i }; rownames.each_with_index { |name, y| printf format, name, *rows[y] }'

screenshot 2016-12-15 01 32 27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment