What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

http://*.com/questions/998979/difference-between-and-in-rails/25617607#25617607

http://*.com/questions/7996695/what-is-the-difference-between-and-in-erb-in-rails

<% %>

Executes the ruby code within the brackets.

<%= %>

Prints something into erb file.

<% -%>

Avoids line break after expression.

<%# %>

Comments out code within brackets; not sent to client (as opposed to HTML comments).

In Ruby 2.1 (not necessarily with Rails), the - removes one trailing newline:

  • the newline must be the first char after the >
  • no spaces are removed
  • only a single newline is removed
  • you must pass the '-' option to use it

Examples:

require 'erb'
ERB.new("<%= 'a' %>\nb").result == "a\nb" or raise
begin ERB.new("<%= 'a' -%>\nb").result; rescue SyntaxError ; else raise; end
ERB.new("<%= 'a' %>\nb" , nil, '-').result == "a\nb" or raise
ERB.new("<%= 'a' -%>\nb" , nil, '-').result == 'ab' or raise
ERB.new("<%= 'a' -%> \nb" , nil, '-').result == "a \nb" or raise
ERB.new("<%= 'a' -%>\n b" , nil, '-').result == 'a b' or raise
ERB.new("<%= 'a' -%>\n\nb", nil, '-').result == "a\nb" or raise
上一篇:Android利用Fiddler进行网络数据抓包,手机抓包工具汇总


下一篇:个人作业2:QQ音乐APP案例分析