Inspect


#1

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)

#2

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?


#3

2006/1/18, dr plutes 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 !


#4

Simple HTML display issue.

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


#5

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


#6

…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


#7

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


#8

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?