# What does 'h' do?

**URL:** <https://www.ruby-forum.com/t/what-does-h-do/51347>\
**Category:** Rails\
**Created:** [December 30, 2005, 12:04am UTC](https://www.ruby-forum.com/t/what-does-h-do/51347 "2005-12-30T00:04:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [December 30, 2005, 12:04am UTC](https://www.ruby-forum.com/t/what-does-h-do/51347/1 "2005-12-30T00:04:30Z")

</div>

I’m sure a real newbie question, but try searching for ‘h’ in any search  
engine and you don’t get far.

I am wondering what the h does in Ruby as in the code below:

\<%= h(truncate(product.description, 80)) %\>

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [December 30, 2005, 12:20am UTC](https://www.ruby-forum.com/t/what-does-h-do/51347/2 "2005-12-30T00:20:08Z")

</div>

On 12/29/05, Mark D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m sure a real newbie question, but try searching for ‘h’ in any search  
> engine and you don’t get far.
> 
> I am wondering what the h does in Ruby as in the code below:
> 
> \<%= h(truncate(product.description, 80)) %\>

h() is shorthand for ‘html\_escape’, which makes sure the content is  
safe for display on an HTML page.

For example, if your controller had some code in it like:  
@example = “  
  
  
”

\<%= @example %\> in a view would put three breaks in a row, when what  
you probably wanted was to display the actual text.

\<%= h(@example) %\> converts those brackets into HTML entities that  
will show up properly.

In general, it’s a good idea to use it whenever you don’t have total  
control over the content, because it will prevent your pages from  
melting.

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [December 30, 2005, 12:23am UTC](https://www.ruby-forum.com/t/what-does-h-do/51347/3 "2005-12-30T00:23:15Z")

</div>

Alias for html\_escape(). Docs for ERb are here:

[http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html](http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html)

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [December 30, 2005, 12:25am UTC](https://www.ruby-forum.com/t/what-does-h-do/51347/4 "2005-12-30T00:25:03Z")

</div>

Jeremy M. wrote:

> Alias for html\_escape(). Docs for ERb are here:
> 
> [http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html](http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html)

Thanks for the response and the resource (in the process of learning…)

I figured it was something like that, I just wanted to be sure.
