title: Load Ruby gem into a user-defined namespace
body: <p>Given a gem that defines top-level classes that clash with some code I have written, is it possible to require the gem in such a way that all its classes are grouped inside a module I can define? For example, if an unsafe_gem defines a class:</p>
  
  <pre><code>class Word
    # ... some code
  end
  </code></pre>
  
  <p>I would need something like:</p>
  
  <pre><code>class Word
    # My word class.
  end
  
  module SafeContainer
    # This obviously doesn't work
    # (i.e. the gem still defines ::Word).
    require 'unsafe_gem'
  end
  </code></pre>
  
  <p>So that I can distinguish between:</p>
  
  <pre><code>Word.new # =&gt; The class I defined.
  SafeContainer::Word.new # =&gt; The class defined by the gem.
  </code></pre>
  
  <p><strong>Some further details:</strong> My code (e.g. the 'Word' class) is already wrapped in its own namespace. However, I want to be able to provide the user with the option of enabling a form of "syntactic sugar", which makes some classes directly accessible under the top-level namespace. This, however, creates a name clash with one of the gems I am using, which defines a top-level class. None of the currently proposed solutions work because the gem actually relies on its globally-defined class being there; so undefining the class breaks the gem. Of course, the gem has more than one file, and individually requiring its files into a module seems to be a very brittle solution. Currently, the only workaround I have found is this:</p>
  
  <pre><code>begin
    # Require the faulty gem.
    require 'rbtagger'
  rescue 
    # If syntactic sugar is enabled...
    if NAT.edulcorated?
      # Temporarily remove the sugar for the clashing class.
      Object.const_unset(:Word); retry
    else; raise; end
  ensure
    # Restore syntactic sugar for the clashing class.
    if NAT.edulcorated?
      Object.const_set(:Word, NAT::Entities::Word)
    end
  end
  </code></pre>
  
  <p>I don't know why, but this makes my toenails curl. Anybody have a better solution?</p>