From: Brian Candler Date: 2007-03-22T05:18:09+09:00 Subject: Re: nil? with block On Wed, Mar 21, 2007 at 09:55:07PM +0900, Michiel de Mare wrote: > If you have a long chain of method calls, but want to check some > temporary values for nils, for instance to raise an exception, > currently the easiest way is with local variables: > > e.g.: > > def zipcode_for_user(user) > address = user.personal_data.address > raise 'no address!' unless address > address.zipcode > end > > If "nil?" is extended to accept a block, and to return self (for > Object) and the result of the yield (for NilClass), the following > notation becomes possible: > > user.personal_data.address.nil?{ raise 'no address!'}.zipcode If your result set doesn't include 'false', then you can use 'or'. Mind you, it gets ugly with the necessary brackets: (user.personal_data.address or raise "no address!").zipcode Especially if you go to multiple levels. It looks a bit too much like LISP... (((user or raise "no user!"). personal_data or raise "no personal_data!"). address or raise "no address!").zipcode Regards, Brian.