Menu

Toshimaru's Blog

Escape ERB tag string in ERB file

How can we output erb tag string like <% erb %> in erb file without being interpreted as erb? You can do this by using double percent <%% %>!

<%% escaped %>

Let’s look at how it changes on irb.

$  irb
>  ERB.new("<%% escaped? %>").result
=> "<% escaped? %>"
>  ERB.new("<% escaped? %>").result
NoMethodError: undefined method `escaped?' for main:Object
	from (erb):1:in `<main>'

<%% becomes string without an error.

See also

Load more