Is there something I am missing?

this s more of a syntax question then a problem persay. I was optimizing
some older code on one of my projects when I hit and interesting
problem. First the Working Code:

placeholder = 0
while(@record_album[placeholder])
temp [email protected]_album[placeholder]
if(temp.parentid == Parent.id)
album_tree +="\t

  • <a
    href="#">#{@record[placeholder][“name”]}
  • \n"
    placeholder+=1
    end
    end

    Now Non working Code:

    placeholder = 0
    while(@record_album[placeholder].parentid == Parent.id)
    album_tree +="\t

  • <a
    href="#">#{@record_album[placeholder][“name”]}
  • \n"
    placeholder+=1
    end

    The non working code gives me a “You have a nil object when you didn’t
    expect it! The error occurred while evaluating nil.parentid” exception.
    I am hoping this is something stupid on my part, but if it isn’t then
    why does the error occur

    Darushin

    Hi Darushin,

    Darushin wrote:

    First the Working Code:

    placeholder = 0
    while(@record_album[placeholder])

    From Pickaxe, p93, “Ruby has a simple definition of truth. Any value
    that
    is not nil or the constant false is true.” The while loop executes
    until
    @record_album[placeholder] evaluates to nil.

    while(@record_album[placeholder].parentid == Parent.id)
    gives me a “You have a nil object when you didn’t
    expect it! The error occurred while evaluating nil.parentid”

    This error occurs because Ruby does not allow you to invoke a method on
    a
    nil object. Your working code avoids this by performing the test for a
    nil
    object prior to attempting to invoke the .parentid method.

    hth,
    Bill

    Thank you so much. I figured there was a reason for this behavior and
    it was driving me nuts to figure out why it was doing this.

    Ben

    Hi Ben,

    Darushin wrote:

    Thank you so much. I figured there was a reason
    for this behavior and it was driving me nuts to figure
    out why it was doing this.

    You’re welcome. Glad to help.

    Best regards,
    Bill

    placeholder = 0
    while(@record_album[placeholder])
    temp [email protected]_album[placeholder]
    if(temp.parentid == Parent.id)
    album_tree +="\t

  • <a
    href="#">#{@record[placeholder][“name”]}
  • \n"
    placeholder+=1
    end
    end

    Ruby has lots of great iterators which can almost always be used in
    place of such iteration schemes using increments:

    @record_album.compact.each do |album|
    next unless album.parentid == Parent.id
    do stuff
    end

    Array#compact will remove any nil entries from your @record_album
    array (I assume it’s an array). Note that the behavior of this one
    would be different from the original in that the original loop breaks
    on the first element that is nil or false and this one will simply
    skip it and continue.

    Cheers,
    Rein

    This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

    | Privacy Policy | Terms of Service | Remote Ruby Jobs