R – why does this comment create a compile error on Ruby on Rails

erbrubyruby-on-rails

there is a line displayed within <pre>

<%= h @stories.inspect %>

and the output was too long, so i changed it to

<%= #h @stories.inspect %>

<% @stories.each do |s| %>
  <%= h s.inspect %>
<% end %>

(commenting out the first line). now the code will fail to compile… saying

compile error
/Users/winterheat/ror/shov2/app/views/stories/index.html.erb:13: syntax error, unexpected kENSURE, expecting ')'
/Users/winterheat/ror/shov2/app/views/stories/index.html.erb:15: syntax error, unexpected kEND, expecting ')'

and if i remove that commented line altogether, the code will work. i thought in some book, it is said that you can comment out some code in ERB like that?

Update: funny if i change it to

<% #h @stories.inspect %>

then it will compile fine… so the displaying of result tag <%= %> doesn't like comments, it seems.

Best Answer

Think of <%= as meaning "add the value of this expression to the output stream". No expression? Syntax error.

Consider

output << @stories.inspect

vs

output <<

Tracking down how <%= is really handled in the erb source may be edifying. I found it worth the effort when I was getting started with Rails.