Homebrew/homebrew — pull request #29947 by rolandwalker (2014-06-07 00:36:17+00:00)
Title: simulate "ruby -EUTF-8:UTF-8", but safe for 1.8

It is not possible to add `-EUTF-8:UTF-8` to the shebang line on older
Rubies. (Even though `/usr/local/bin/brew` does `exec ruby brew.rb`, the
shebang line in `brew.rb` is consulted for switches.)

Without `-EUTF-8:UTF-8`, Ruby 1.9-2.x, and Homebrew, are influenced by
the LANG environment variable (and perhaps others). Specifically, when
LANG is unset, `Encoding::default_external` reverts to `US-ASCII`. See
the following (Mavericks):
```bash
$ LANG=en_US.UTF-8 /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby -e 'puts Encoding.default_external; puts ARGV.first.encoding.name' one
UTF-8
UTF-8
$ LANG= /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby -e 'puts Encoding.default_external; puts ARGV.first.encoding.name' one
US-ASCII
US-ASCII
```

This PR acts as early as possible after invocation to simulate `-EUTF-8:UTF-8`.
Waiting to do so increases the risk of heterogeneous encodings. There may be
other special variables like `ARGV` which I have not addressed, but need explicit
encoding here. For example, elements of `ENV` will still be encoded as `US-ASCII`.

An alternative approach would be to detect the ruby version in `/usr/local/bin/brew`,
and selectively apply `-E` arguments on `exec`.