Women in Technology

Hear us Roar



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?
Full Threads Oldest First

Showing messages 1 through 2 of 2.

  • Curt Hibbs photo Ruby phantoms
    2005-06-04 08:17:38  Curt Hibbs | O'Reilly AuthorO'Reilly Blogger [View]

    This was my fault -- a bug. Take a look at part 2 of this article. At the very beginning I mention correcting this problem and I have a zip file you can download with the working source code for part 1. You can use this source code to compare with yours. You can find part 2 here:

    http://www.onlamp.com/pub/a/onlamp/2005/03/03/rails.html
    • Ruby phantoms
      2005-06-06 11:58:38  Meindert [View]

      Well, I don't regard the undefinedness of recipe.catory.name in the new record as a bug, but just as work in progress. No problem with that.
      My curiosity concerns the outcome of the defined? operator.
      • In the existing records: recipe.category.nil? => false, (defined? recipe.category) => "method", defined? recipe.category.name => nil and recipe.category.name => some_string.

      • In the new record: recipe.category.nil? => true, (defined? recipe.category) => "method", defined? recipe.category.name => nil and recipe.category.name => nil.

      Isn't that weird? I suspect defined? doesn't always return what you might expect. As long as defined? won't clearly tell me about the nature of recipe.category and recipe.category.name, I'm inclined to ask the maker of these contraptions about his perception.
      (In plain Ruby, I could not reproduce this behaviour of defined?. I created an object.object2.method_of_object2: defined? object.object2 => "method" and defined? object.object2.method_of_object2 => "method". This seems correct to me.)