Xavier Noria
8/18/2007 12:50:00 PM
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