Bug #17846
closedPercent mode changes the output from ERB beyond what is documented
Description
/ # cat repro.erb
<% # %>x
y
/ # erb repro.erb
y
/ # erb -P repro.erb
/ #
Based on documentation of -P (disabling percent mode), this change in behavior
is unexpected.
Updated by xtkoba (Tee KOBAYASHI) over 3 years ago
Apologies if intentional, but it seems that whitespace between <%
and #
can be troublesome:
https://makandracards.com/makandra/3533-erb-templates-and-comments
Updated by k0kubun (Takashi Kokubun) over 3 years ago
- Status changed from Open to Rejected
-P disable ruby code evaluation for lines beginning with %
"lines beginning with %
" means lines beginning with %
. Your lines start with <
or y
, so -P
is irrelevant to your input.
Updated by k0kubun (Takashi Kokubun) over 3 years ago
- Status changed from Rejected to Feedback
Oh sorry, I guess that's why it shouldn't change the output lol. will take a look
Updated by k0kubun (Takashi Kokubun) over 3 years ago
- Status changed from Feedback to Rejected
So the trick is:
$ erb -x repro.erb
#coding:UTF-8
_erbout = +''; # ; _erbout.<< "x\n".freeze
; _erbout.<< "y\n".freeze
; _erbout
$ erb -x -P repro.erb
#coding:UTF-8
_erbout = +''; # ; _erbout.<< "x\ny\n".freeze
; _erbout
At a glance, the generated code seems broken. However, @xtkoba (Tee KOBAYASHI) made the point; <%#
is a special token of ERB that allows a Ruby comment, whereas <% #
is a Ruby expression with a comment. While it sounds strange that it's impacted by -P
, whether -P
or not, writing a comment in the middle of <% %>
is currently not supported. For your case, you should just use <%#
instead of <% #
.
Updated by graywolf (Gray Wolf) over 3 years ago
k0kubun (Takashi Kokubun) wrote in #note-4:
[..] writing a comment in the middle of
<% %>
is currently not supported.
I don't really have an issue with this resolution, however, is it documented
somewhere? I cannot find this mentioned anywhere in the documentation nor the
source code.
Updated by k0kubun (Takashi Kokubun) over 3 years ago
Maybe not. Now I did https://github.com/ruby/erb/commit/b58b188028fbb403f75d48d62717373fc0908f7a.
Updated by graywolf (Gray Wolf) over 3 years ago
k0kubun (Takashi Kokubun) wrote in #note-6:
Maybe not. Now I did https://github.com/ruby/erb/commit/b58b188028fbb403f75d48d62717373fc0908f7a.
Thank you :)