Forum: Ruby on Rails Inspect

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Da P. (Guest)
on 2006-01-18 22:33
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 %><br>
     <% 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)
Kevin O. (Guest)
on 2006-01-18 22:46
> 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?
Francois B. (Guest)
on 2006-01-18 22:46
(Received via mailing list)
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 !
Da P. (Guest)
on 2006-01-18 22:51
Simple HTML display issue.

Too bad this forum doesn't allow poster to delete own posts, sorry!
Da P. (Guest)
on 2006-01-18 22:59
> ...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
Rob B. (Guest)
on 2006-01-19 00:26
(Received via mailing list)
At 1/18/2006 03:33 PM, you wrote:
>In show.rhtml:
>      <% @recipe.inspect.split(',').each do |object| %>
>      <%= object %><br>
>      <% end %>

<%=h object %><
if you html_escape() it so that you don't have "<title ..." perhaps
it will be better

-Rob
Da P. (Guest)
on 2006-01-19 01:46
Rob B. wrote:
> At 1/18/2006 03:33 PM, you wrote:
>>In show.rhtml:
>>      <% @recipe.inspect.split(',').each do |object| %>
>>      <%= object %><br>
>>      <% 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?
Rob B. (Guest)
on 2006-01-19 02:30
(Received via mailing list)
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
This topic is locked and can not be replied to.