From: dblack@... Date: 2006-03-03T00:52:57+09:00 Subject: Re: cleaner Ruby in a rails view Hi -- On Fri, 3 Mar 2006, James Edward Gray II wrote: > On Mar 2, 2006, at 9:33 AM, Charlie Bowman wrote: > >> I know this is the ruby list and not the rails list but it would be nice >> if we didn't have to add code to our views to rescue nils. I guess you >> could add the method each if you to the nil class right? > > You can, sure. Whether or not it is a good idea is the question. ;) > > Currently, when you call each() on nil, it explodes. This is a good thing, > because it takes you right to the problem. You were expecting to have an > Enumerable, but you really have a nil. > > If we take that away, Ruby will start silently accepting what are most likely > mistakes. While that may keep you from typing (enum || []).each... in some > places, it will prevent Ruby from catching real errors in others. Better > that we explicitly state that nil is okay in this case, and let Ruby watch > our back for us the rest of the time, don't you think? Yes, definitely. Almost every time I get one of these "undefined method `x' for nil" messages, it's a message I'm glad I got because it shows me something was wrong. It may be something wrong with my code, or it may just be that something somewhere else returns nil rather than [] (or whatever) on failure, and I hadn't realized it. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black