Project

General

Profile

Actions

Bug #17846

closed

Percent mode changes the output from ERB beyond what is documented

Added by graywolf (Gray Wolf) about 1 year ago. Updated about 1 year ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [x86_64-linux-musl]
[ruby-core:103690]

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) about 1 year 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) about 1 year 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) about 1 year 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) about 1 year 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) about 1 year 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 graywolf (Gray Wolf) about 1 year ago

k0kubun (Takashi Kokubun) wrote in #note-6:

Maybe not. Now I did https://github.com/ruby/erb/commit/b58b188028fbb403f75d48d62717373fc0908f7a.

Thank you :)

Actions

Also available in: Atom PDF