https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112017-11-09T19:29:55ZRuby Issue Tracking SystemRuby master - Bug #14095: erb ignores attempt to set encodinghttps://bugs.ruby-lang.org/issues/14095?journal_id=677312017-11-09T19:29:55Zgraywolf (Gray Wolf)
<ul></ul><p>Expected output was</p>
<pre><code>$ erb -U
<%= "a".encoding %>
^D
UTF-8
</code></pre>
<p>Ruby works that way without setting anything</p>
<pre><code>$ ruby
puts "a".encoding
^D
UTF-8
</code></pre>
<p>Why doesn't .erb have the same default?</p> Ruby master - Bug #14095: erb ignores attempt to set encodinghttps://bugs.ruby-lang.org/issues/14095?journal_id=677332017-11-10T02:49:28Zhsbt (Hiroshi SHIBATA)hsbt@ruby-lang.org
<ul><li><strong>Assignee</strong> set to <i>k0kubun (Takashi Kokubun)</i></li></ul> Ruby master - Bug #14095: erb ignores attempt to set encodinghttps://bugs.ruby-lang.org/issues/14095?journal_id=677352017-11-10T02:50:07Zhsbt (Hiroshi SHIBATA)hsbt@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li></ul> Ruby master - Bug #14095: erb ignores attempt to set encodinghttps://bugs.ruby-lang.org/issues/14095?journal_id=677672017-11-10T13:59:12Zk0kubun (Takashi Kokubun)takashikkbn@gmail.com
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>Applied in changeset trunk|r60739.</p>
<hr>
<p>bin/erb: change template file encoding to UTF-8</p>
<p>Unlike Ruby source file encoding (script encoding) whose default is<br>
changed to UTF-8 in Ruby 2.0 (Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Default Ruby source file encoding to utf-8 (Closed)" href="https://bugs.ruby-lang.org/issues/6679">#6679</a>), template's file encoding<br>
given to erb(1) has been ASCII-8BIT since ERB supports m17n at r21170.</p>
<p>Like Ruby source file encoding, erb template file encoding should be<br>
UTF-8 in Ruby 2.</p>
<p>[Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: erb ignores attempt to set encoding (Closed)" href="https://bugs.ruby-lang.org/issues/14095">#14095</a>] <a href="/issues/14095">[ruby-core:83708]</a></p> Ruby master - Bug #14095: erb ignores attempt to set encodinghttps://bugs.ruby-lang.org/issues/14095?journal_id=677682017-11-10T14:05:59Zk0kubun (Takashi Kokubun)takashikkbn@gmail.com
<ul></ul><p>Hi Gray,</p>
<p>First of all, -U option of erb(1) is just the same as ruby(1)'s one, which sets both external and internal encoding to UTF-8. As those encodings are for IO, -U (or -E) is not related to the described behavior.</p>
<p>The actual cause that matters is source file encoding (script encoding, which affects encoding of string literal in the file) counterpart for erb(1) command. As m17n of erb(1) is implemented in Ruby 1.9, it's the same as Ruby 1.9's default source file encoding: ASCII-8BIT. But Ruby 2's default source file encoding is changed to UTF-8, I thought erb(1)'s one should be changed in the same way. Thus I fixed it in r60739.</p> Ruby master - Bug #14095: erb ignores attempt to set encodinghttps://bugs.ruby-lang.org/issues/14095?journal_id=677702017-11-10T14:18:05Zk0kubun (Takashi Kokubun)takashikkbn@gmail.com
<ul></ul><p>Ah, one more comment is that you can use magic comment to change source file encoding of erb template before and after that change:</p>
<pre><code class="erb syntaxhl" data-language="erb">$ ruby -v
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
$ cat ascii.erb
<span class="cp"><%=</span> <span class="s1">''</span><span class="p">.</span><span class="nf">encoding</span> <span class="cp">%></span>
$ erb -T 1 ascii.erb
ASCII-8BIT
$ cat utf-8.erb
<span class="c"><%# coding: UTF-8 %></span>
<span class="cp"><%=</span> <span class="s1">''</span><span class="p">.</span><span class="nf">encoding</span> <span class="cp">%></span>
$ erb -T 1 utf-8.erb
UTF-8
</code></pre> Ruby master - Bug #14095: erb ignores attempt to set encodinghttps://bugs.ruby-lang.org/issues/14095?journal_id=677722017-11-10T15:46:49Zgraywolf (Gray Wolf)
<ul></ul><p>Thank you for quick fix and your answer, I've tried the magic comment thing base on what I googled, to be precise <code><% # -*- coding: UTF-8 -*- %></code> which didn't work. Your version <code><%# coding: UTF-8 %></code> does work so I can now use <code>?猫</code> without getting parser error. Yeeey!</p>