title: Overriding getter of OpenStruct in order to print it as a Hash
body: <p>GOAL: Values of an OpenStruct object should be printed as a hash rather than an object</p>
  
  <p>POSSIBLE SOLUTION: Override getter of the OpenStruct class</p>
  
  <p><code>MyOpenStruct</code> overrides <code>new</code>, <code>to_h</code> and <code>[]</code> of <code>OpenStruct</code>.</p>
  
  <pre><code>class MyOpenStruct &lt; OpenStruct
      def initialize(object=nil)
          @table = {}
          @hash_table = {}
  
          if object
              object.each do |k,v|
                  if v.is_a?(Array)
                      other = Array.new()
                      v.each { |e| other.push(self.class.new(entry)) }
                      v = other
                  end
                  @table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
                  @hash_table[k.to_sym] = v
                  new_ostruct_member(k)
              end
          end
      end
  
      def [](val)
          @hash_table[val.to_sym]
      end
  end
  </code></pre>
  
  <p>But overriding <code>[]</code> is not making any difference. E.g.</p>
  
  <pre><code>irb(main):007:0&gt; temp = MyOpenStruct.new({"name"=&gt;"first", "place"=&gt;{"animal"=&gt;"thing"}})
  =&gt; #&lt;MyOpenStruct name="first", place=#&lt;MyOpenStruct animal="thing"&gt;&gt;
  irb(main):008:0&gt; temp.name
  =&gt; "first"
  irb(main):009:0&gt; temp.place
  =&gt; #&lt;MyOpenStruct animal="thing"&gt;
  irb(main):010:0&gt; temp["place"]
  =&gt; {"animal"=&gt;"thing"}
  irb(main):011:0&gt; temp[:place]
  =&gt; {"animal"=&gt;"thing"}
  irb(main):012:0&gt; temp
  =&gt; #&lt;MyOpenStruct name="first", place=#&lt;MyOpenStruct animal="thing"&gt;&gt;
  </code></pre>
  
  <p>Only when I access the keys using <code>[]</code> a hash is returned!!</p>
  
  <p>How can I correct this??</p>