# Inspect

**URL:** <https://www.ruby-forum.com/t/inspect/95566>\
**Category:** Rails\
**Created:** [January 18, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/inspect/95566 "2006-01-18T21:33:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 18, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/inspect/95566/1 "2006-01-18T21:33:58Z")

</div>

I am seeing a curious anomaly with the inspect method. It displays one  
of the objects in a way I don’t understand. I’ll use the Recipes example  
to illustrate.

First, I’ll describe the recipes table, then show how the @recipe  
variable is being created in the controller and passed in to the show  
view, and then show how I’m using the ‘inspect’ method to display the  
@recipe object.

mysql\> describe recipes;  
±-------------±-------------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±-------------±-------------±-----±----±--------±---------------+  
| id | int(11) | NO | PRI | NULL | auto\_increment |  
| title | varchar(255) | NO | | | |  
| instructions | text | YES | | NULL | |  
| description | varchar(255) | YES | | NULL | |  
| date | date | YES | | NULL | |  
| category\_id | int(6) | YES | | NULL | |  
±-------------±-------------±-----±----±--------±---------------+

In class RecipeController:  
def show  
@recipe = Recipe.find(params[:id])  
end

In show.rhtml:  
\<% @recipe.inspect.split(’,’).each do |object| %\>  
\<%= object %\>  
  
\<% end %\>

Now, for a particular instance, this displays the following on the web  
page for the show view:

```
 #"Killer Mushrooms"
 "date"=>"2006-01-11"
 "id"=>"3"
 "category_id"=>"1"
 "description"=>"Last one you'll ever need"
 "instructions"=>" Serve randomly collected forest mushrooms"}>

```

Note the first line. That is the “title” column. WHy is it displayed as  
#“Killer Mushrooms” instead of “title”=\>“Killer Mushrooms” ??

Now, if I include the following in show.rhtml:

```
 title = <%= @recipe.title %><br>

```

then this is displayed:

```
 title = Killer Mushrooms

```

So clearly the object contains the ‘title’ method.

Can anybody help me understand why this object being displayed by  
‘inspect’ in this way?

(PS: The tokenization performed by the split has no effect, the same  
thing is displayed if it is not tokenized, just all on the same line.)

- Paint me Curious (Ruby)

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 18, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/inspect/95566/2 "2006-01-18T21:46:07Z")

</div>

> Now, for a particular instance, this displays the following on the web  
> page for the show view:
> 
> ```
> #"Killer Mushrooms"
> "date"=>"2006-01-11"
> "id"=>"3"
> "category_id"=>"1"
> "description"=>"Last one you'll ever need"
> "instructions"=>" Serve randomly collected forest mushrooms"}>
> 
> ```

What does the HTML for this look like?

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 18, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/inspect/95566/3 "2006-01-18T21:46:54Z")

</div>

2006/1/18, dr plutes [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Can anybody help me understand why this object being displayed by  
> ‘inspect’ in this way?

Did you know about debug ?

\<%= debug(@recipe) %\>

Also, if you view source, what do you see ? Because inspect will  
return:

\<#0x39801 Recipe…

as the first token.

Bye !

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 18, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/inspect/95566/4 "2006-01-18T21:51:44Z")

</div>

Simple HTML display issue.

Too bad this forum doesn’t allow poster to delete own posts, sorry!

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 18, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/inspect/95566/5 "2006-01-18T23:26:26Z")

</div>

At 1/18/2006 03:33 PM, you wrote:

> In show.rhtml:  
> \<% @recipe.inspect.split(’,’).each do |object| %\>  
> \<%= object %\>  
>   
> \<% end %\>

\<%=h object %\>\<  
if you html\_escape() it so that you don’t have “\<title …” perhaps  
it will be better

-Rob

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 18, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/inspect/95566/6 "2006-01-18T21:59:16Z")

</div>

> …if you view source, what do you see ? Because inspect will  
> return:
> 
> \<#0x39801 Recipe…
> 
> as the first token.

Thanks!

Indeed, when I do a puts to the console, I see displayed there the  
following:

#\<Recipe:0x37c31a8 @attributes={“title”=\>“Killer Mushrooms”,  
“date”=\>“2006-01-11”, “id”=\>“3”, “category\_id”=\>“1”,  
“description”=\>“Last one you’ll ever need”, “instructions”=\>" Serve  
randomly collected forest mushrooms"}\>

And as a result, the following string:  
\<Recipe:0x38af620 @attributes={“title”=\>  
ends up being ‘invisible’.

> Did you know about debug ?
> 
> \<%= debug(@recipe) %\>

Double thanks! Much appreciated

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 19, 2006, 1:30am UTC](https://www.ruby-forum.com/t/inspect/95566/7 "2006-01-19T01:30:30Z")

</div>

At 1/18/2006 06:46 PM, you wrote:

> > -Rob
> 
> Indeed - that does the trick. Thanks!
> 
> Does the \<%=h object %\> display the contents of any object ‘as-is’  
> without its contents being confused by the browser to be a tag or other  
> html element?

h is short for html\_escape and Ruby lets you skip the ()'s when you  
don’t need them, so the short answer is:  
Yes.

Any time you have content that could be taken the wrong way by the  
browser, use html\_escape:

\<%= html\_escape(object) %\>

but you’re much more likely to see (and do)

\<%=h object %\>

-Rob

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 19, 2006, 12:46am UTC](https://www.ruby-forum.com/t/inspect/95566/8 "2006-01-19T00:46:35Z")

</div>

Rob B. wrote:

> At 1/18/2006 03:33 PM, you wrote:
> 
> > In show.rhtml:  
> > \<% @recipe.inspect.split(’,’).each do |object| %\>  
> > \<%= object %\>  
> >   
> > \<% end %\>
> 
> \<%=h object %\>\<  
> if you html\_escape() it so that you don’t have “\<title …” perhaps  
> it will be better
> 
> -Rob

Indeed - that does the trick. Thanks!

Does the \<%=h object %\> display the contents of any object ‘as-is’  
without its contents being confused by the browser to be a tag or other  
html element?
