https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112018-08-29T18:40:45ZRuby Issue Tracking SystemRuby master - Bug #15044: ENV encoding not UTF-8 by defaulthttps://bugs.ruby-lang.org/issues/15044?journal_id=737912018-08-29T18:40:45Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>If I put this into a .rb file:</p>
<pre><code>puts 'secret'.encoding
ENV['PASS'] = 'secret'
puts ENV['PASS'].encoding
</code></pre>
<p>On my system I get these two Strings output:</p>
<pre><code>UTF-8
ISO-8859-1
</code></pre>
<p>My environment is, aka my current locale, iso-8859-1, so the results that<br>
I get seem correct. I can change the UTF-8 default encoding if I use a<br>
shebang line in the .rb file, which I normally do, so all my encodings are<br>
the same (ISO-8859-1; regexes used to behave a bit oddly sometimes but I<br>
am not sure if that has changed or not).</p>
<p>I think ENV behaves a litle bit differently upon an<br>
assignment.</p>
<p>If I use a shebang line in a .rb file that includes the above unicode<br>
character (this weird L), then all string encodings in that .rb file<br>
are also ISO-8859-1, so I am not sure if there is any bug at all.<br>
It may be more related to IRB perhaps? I skipped testing on IRB mostly<br>
because .rb files have a "higher weight" than code put through IRB.</p>
<p>The documentation does not mention what happens with encodings when<br>
these are assigned to an ENV key, though:</p>
<p><a href="https://ruby-doc.org/core-2.5.1/ENV.html" class="external">https://ruby-doc.org/core-2.5.1/ENV.html</a></p>
<p>Perhaps it has more to do with IRB, in which case it could be added<br>
there:</p>
<p><a href="http://ruby-doc.org/stdlib-2.5.1/libdoc/irb/rdoc/IRB.html" class="external">http://ruby-doc.org/stdlib-2.5.1/libdoc/irb/rdoc/IRB.html</a></p>
<p>And of course it may be that there is indeed a bug. You can try to<br>
test with a standalone .rb file though and, if necessary, with a<br>
specific shebang comment.</p> Ruby master - Bug #15044: ENV encoding not UTF-8 by defaulthttps://bugs.ruby-lang.org/issues/15044?journal_id=830292019-12-09T10:20:39Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>It is intentional according to naruse. The encoding of ENV depends on the environment variable LANG.</p> Ruby master - Bug #15044: ENV encoding not UTF-8 by defaulthttps://bugs.ruby-lang.org/issues/15044?journal_id=830302019-12-09T10:21:55Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>The assigned value to <code>ENV</code> are stored in the process's environment variable.</p>
<p>The encoding of <code>ENV[key]</code> is set as locale.<br>
You can get the locale encoding by <code>Encoding.find("locale")</code> which is decided based on <code>Encoding.locale_charmap</code> which is affected by <code>ENV["LANG"]</code> and <code>ENV["LC_ALL"]</code>.</p>
<p>Note that <code>ENV["PATH"]</code> is returned as filesystem encoding but it is the same as locale encoding on Unix.</p>