| Article: |
Rolling with Ruby on Rails | |
| Subject: | Ruby phantoms | |
| Date: | 2005-06-04 05:47:15 | |
| From: | Meindert | |
|
After reading this appetizer Rolling with Ruby on Ralis last week, I immediately started downloading all stuff and building the part one application. Without much knowledge of Ruby I'm afraid. As could be expected, it ended with a undefined method recipe.category.name in list.rhtml, after a new recipe was added. At that moment, I did no further reading (stupid me), but tried to repair it by myself, adding ... if defined? recipe.category.name to it. That did not work, because defined? recipe.category.name always returns nil, and defined? recipe.category always returns method. I added ... unless recipe.category.nil? instead, which did the trick and looks more elegant.But it leaves me puzzled: recipe.category and recipe.category.name, what kind of Ruby things are they? |
||
Showing messages 1 through 2 of 2.
-
Ruby phantoms
2005-06-04 08:17:38 Curt Hibbs |
[View]
-
Ruby phantoms
2005-06-06 11:58:38 Meindert [View]
Well, I don't regard the undefinedness ofrecipe.catory.namein the new record as a bug, but just as work in progress. No problem with that.
My curiosity concerns the outcome of thedefined?operator.
- In the existing records:
recipe.category.nil? => false,(defined? recipe.category) => "method",defined? recipe.category.name => nilandrecipe.category.name =>some_string. - In the new record:
recipe.category.nil? => true,(defined? recipe.category) => "method",defined? recipe.category.name => nilandrecipe.category.name => nil.
Isn't that weird? I suspectdefined?doesn't always return what you might expect. As long asdefined?won't clearly tell me about the nature ofrecipe.categoryandrecipe.category.name, I'm inclined to ask the maker of these contraptions about his perception.
(In plain Ruby, I could not reproduce this behaviour ofdefined?. I created anobject.object2.method_of_object2:defined? object.object2 => "method"anddefined? object.object2.method_of_object2 => "method". This seems correct to me.) - In the existing records:



http://www.onlamp.com/pub/a/onlamp/2005/03/03/rails.html