title: How do I check if a class is defined?
body: <p>How do I turn a string into a class name, but only if that class already exists?</p>
  
  <p>If Amber is <em>already</em> a class, I can get from a string to the class via:</p>
  
  <pre><code>Object.const_get("Amber")
  </code></pre>
  
  <p>or (in Rails)</p>
  
  <pre><code>"Amber".constantize
  </code></pre>
  
  <p>But either of these will fail with <code>NameError: uninitialized constant Amber</code> if Amber is not already a class.</p>
  
  <p>My first thought is to use the <code>defined?</code> method, but it doesn't discriminate between classes that already exist and those that don't:</p>
  
  <pre><code>&gt;&gt; defined?("Object".constantize)
  =&gt; "method"
  &gt;&gt; defined?("AClassNameThatCouldNotPossiblyExist".constantize)
  =&gt; "method"
  </code></pre>
  
  <p>So how do I test if a string names a class before I try to convert it?  (Okay, how about a <code>begin</code>/<code>rescue</code> block to catch NameError errors?  Too ugly?  I agree...)</p>