New safe navigation operator

I recently started learning ruby after coming from swift so I love that
ruby now has the safe navigation operator “&.” So now you can safely do
the following:

foo = nil
foo&.bar

Which is similar to:

foo = nil
foo.bar unless foo.nil?

I want to know if it would be possible to make one, let’s say “>.” That
would be short hand for the following:

foo.bar if foo.respond_to? :bar

So you can then safely do:

foo>.bar

I don’t think so. Because when Ruby interpreter will evaluate this
expression:

foo>.bar

it actually will like this:

foo.>(.bar)

And .bar will raise an exception.

Technically, you can write something like this:

define_method ‘>.’ and call it that way:

send :’>.’ but it looks like a dirty hack :slight_smile:

P.S. Why you need this behaviour at all? If you have not nil in foo and
want to check response to bar that is bar will be private method or not.
But if it’s private, you should never call it from foo.