3

I've been following Agile Web Development with Rails, and their code suddenly switches from the use of <%= %> to <% %>.

Code Example 1:

<%= 1+2 %>

Code Example 2:

<% for file in @files %>    
file name is: <%= file %>
<% end %>

My question is, for the second code sample why can I not use <%= %>?

Thanks!

Andrew
  • 3,501
  • 8
  • 35
  • 54

1 Answers1

3

In ERB (embedded ruby) syntax, <%= %> is shorthand for "perform the following ruby code AND THEN print the result". So in the first example, it will print the result of the operation 1 + 2, 3.

The second example shows a for loop which will iterate and print the contents between the for declaration and its corresponding <% end %>.

To answer your question, the expression for file in @files itself doesn't return anything worth printing so there's no need to use <%= %> and in fact doing so can cause a hard-to-track-down bug.

allenan
  • 174
  • 1
  • 6