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 =@record_album[placeholder] if(temp.parentid == Parent.id) album_tree +="\t<li><a href=\"\#\">#{@record[placeholder]["name"]}</a></li>\n" placeholder+=1 end end Now Non working Code: placeholder = 0 while(@record_album[placeholder].parentid == Parent.id) album_tree +="\t<li><a href=\"\#\">#{@record_album[placeholder]["name"]}</a></li>\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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
rein.henrichs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-27 15:14 UTC
Re: Is there something I am missing?
> placeholder = 0 > while(@record_album[placeholder]) > temp =@record_album[placeholder] > if(temp.parentid == Parent.id) > album_tree +="\t<li><a > href=\"\#\">#{@record[placeholder]["name"]}</a></li>\n" > placeholder+=1 > end > endRuby 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---