Why does Object::try work if it's sent to a nil object? <p>If you try to call a method on a <code>nil</code> object in Ruby, a <code>NoMethodError</code> exception arises with the message:</p> <pre><code>"undefined method ‘...’ for nil:NilClass" </code></pre> <p>However, there is a <a href="http://apidock.com/rails/Object/try"><code>try</code></a> method in Rails which just return <code>nil</code> if it's sent to a <code>nil</code> object:</p> <pre><code>require 'rubygems' require 'active_support/all' nil.try(:nonexisting_method) # no NoMethodError exception anymore </code></pre> <p>So how does <code>try</code> work internally in order to prevent that exception?</p>