Roger Pack wrote:
> On Wed, Feb 18, 2009 at 2:35 PM, Joel VanderWerf
> <vjoel / path.berkeley.edu> wrote:
>> Roger Pack wrote:
>>>>>> Then how it is different from
>>>>>> foo.bar if foo.respond_to?(:bar)
>>>>>>
>>>>>> ?
>>>>> You don't have to write foo and bar twice :)
>>>>>
>>>>> I'm with you in thinking that adding another syntax
>>>>> foo.?bar
>>>>> seems a little much when a new method will do.
>>>>> -=r
>>>> Agree on both points. But what does the new method return?
> 
> Oh I gotcha.
> It would return  something like Yusuke Endoh's suggestion...
> Implementation image:
> 
>  class Object
>    def if_not_nil
>      self
>    end
>  end
> 
>  class NilClass
>    def if_not_nil
>      obj = BasicObject.new
>      def obj.method_missing(*)
>        nil
>      end
>      obj
>    end
>  end
> 
> not sure how the if_responds_to method would look like, exactly.
> -=r

Chaining would go like this then?

x.if_not_nil.do_foo.if_not_nil.do_bar

We'd have to repeat #if_not_nil at each link in the chain, and it would 
become ambiguous whether the second #if_not_nil was intended to handle 
x==nil or x.do_foo==nil (or both).

Or maybe the method_missing implementation should return self? Then we 
could write:

x.if_not_nil.do_foo.do_bar

Then if you explicitly want to handle a nil result from do_foo, you 
could do so unambiguously:

x.if_not_nil.do_foo.if_not_nil.do_bar

-- 
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407