From: Xavier Noria Date: 2007-08-18T21:50:26+09:00 Subject: Re: Emulating a Groovy feature? On Aug 18, 2007, at 2:36 PM, John Wells wrote: > Guys, > > There's something called Safe Navigation in Groovy (http:// > groovy.codehaus.org/Statements#Statements-Safenavigation) that I > find very appealing. > > So, Groovy will allow you to safely walk an object graph, even if > that graph has nulls in it (the walk will short circuit): > > def foo = null > def bar = foo?.something?.myMethod() > assert bar == null > > Is there an easy way to hack an equivalent out in Ruby? Sure, NilClass can be reopened just fine: class NilClass def method_missing(name, *args) nil end end nil.invoices.find(5) # -> nil Reopening NilClass is something really handy sometimes. -- fxn