How to HTML encode/escape a string? Is there a built-in? How to HTML encode/escape a string? Is there a built-in? ruby ruby

How to HTML encode/escape a string? Is there a built-in?


Checkout the Ruby CGI class. There are methods to encode and decode HTML as well as URLs.

CGI::escapeHTML('Usage: foo "bar" <baz>')# => "Usage: foo "bar" <baz>"


The h helper method:

<%=h "<p> will be preserved" %>


In Ruby on Rails 3 HTML will be escaped by default.

For non-escaped strings use:

<%= raw "<p>hello world!</p>" %>