title: Ruby core extensions with modules
body: <p>Basically I have two modules: <code>CoreExtensions::CamelcasedJsonString</code> and <code>…::CamelcasedJsonSymbol</code>. The latter one overrides the <code>Symbol#to_s</code>, so that the method returns a <code>String</code> which is extended with the first module. I don't want every string to be a <code>CamelcasedJsonString</code>. This is the reason why I try to apply the extension instance specific.</p>
  
  <p>My problem is, that <code>Symbol#to_s</code> seems to be overridden again after I included my module (the last spec fails):</p>
  
  <pre><code>require 'rubygems' if RUBY_VERSION &lt; '1.9'
  require 'spec'
  
  module CoreExtensions
  
    module CamelcasedJsonString; end
  
    module CamelcasedJsonSymbol
  
      alias to_s_before_core_extension to_s
      def to_s(*args)
        to_s_before_core_extension(*args).extend(CamelcasedJsonString)
      end
  
    end
    ::Symbol.send :include, CamelcasedJsonSymbol
  
  end
  
  describe Symbol do
  
    subject { :chunky_bacon }
  
    it "should be a CamelcasedJsonSymbol" do
      subject.should be_a(CoreExtensions::CamelcasedJsonSymbol)
    end
  
    it "should respond to #to_s_before_core_extension" do
      subject.should respond_to(:to_s_before_core_extension)
    end
  
    specify "#to_s should return a CamelcasedJsonString" do
      subject.to_s.should be_a(CoreExtensions::CamelcasedJsonString)
    end
  
  end
  </code></pre>
  
  <p>However the following example works:</p>
  
  <pre><code>require 'rubygems' if RUBY_VERSION &lt; '1.9'
  require 'spec'
  
  module CoreExtensions
    module CamelcasedJsonString; end
  end
  
  class Symbol
    alias to_s_before_core_extension to_s
    def to_s(*args)
      to_s_before_core_extension(*args).extend(CoreExtensions::CamelcasedJsonString)
    end
  end
  
  describe Symbol do
  
    subject { :chunky_bacon }
  
    it "should respond to #to_s_before_core_extension" do
      subject.should respond_to(:to_s_before_core_extension)
    end
  
    specify "#to_s should return a CamelcasedJsonString" do
      subject.to_s.should be_a(CoreExtensions::CamelcasedJsonString)
    end
  
  end
  </code></pre>
  
  <hr>
  
  <p><em>Update: Jan 24, 2010</em></p>
  
  <p>The background of my problem is that I try to convert a huge nested hash
  structure into a JSON string. Each key in this hash is a Ruby <code>Symbol</code> in the
  typical underscore notation. The JavaScript library which consumes the JSON
  data expects the keys to be strings in camelcase notation. I thought that
  overriding the <code>Symbol#to_json</code> method might be the easiest way. But that
  didn't work out since <code>Hash#to_json</code> calls first <code>#to_s</code> and afterwards
  <code>#to_json</code> on each key. Therefore I thought it might be a solution to extend
  all Strings returnd by <code>Symbol#to_s</code> with a module which overrides the
  <code>#to_json</code> method of this specific string instance to return a string that has
  a <code>#to_json</code> method which returns itself in camelcase notation.</p>
  
  <p>I'm not sure if there is an easy way to monkey patch <code>Hash#to_json</code>.</p>
  
  <p>If someone wants to take a look into the JSON implementation I'm using, here is the link: <a href="http://github.com/flori/json/blob/master/lib/json/pure/generator.rb" rel="nofollow noreferrer">http://github.com/flori/json/blob/master/lib/json/pure/generator.rb</a> (lines 239 and following are of interest)</p>