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)