On Nov 13, 2005, at 5:57 PM, listrecv / gmail.com wrote:
> Later, you could do:
> a = o.a_method
> a.address.zip #--> No errors, just nil's
>
> instead of
> if a && a.address && a.address.zip
>   a.address.zip
> else
>   nil
> end

Take a look at the NullObject pattern: http://c2.com/cgi/wiki?NullObject
you might be able to come up with some specific null objects for your
scenario.

I'd shoot for something specific to you problem but Ruby of course
will let you create a "generic" null object:

class Object
   def mynull?
     false
   end
end

class MyNull
   def method_missing(name, *args, &block)
     self
   end

   def mynull?
     true
   end

   def to_s
     ""
   end
end

You can't depend on the object testing as false, use #mynull? if
you need to know what it is.




Gary Wright