From: hemant Date: 2010-02-04T12:14:16+09:00 Subject: Re: hooking subscript operations in a hash On Thu, Feb 4, 2010 at 8:41 AM, Ralph Shnelvar wrote: > RD> On Wed, Feb 3, 2010 at 9:38 PM, Ralph Shnelvar wrote: >>> In order to help debug something, I'd like to hook the hash subscript operation. > >>> I've tried this (and several variants) in irb ... but it doesn't do what I want it to. > > >>> module RalphMod >>>  def []=(index, value) >>>    p index >>>    p value >>>    if index == 'PATH_INFO' and value == '/undefined' >>>      puts 'found it!' >>>    end >>>    super index, value >>>  end >>> end > >>> class Hash >>>  extend RalphMod >>> end > >>> env = {} >>> env['X'] = 'Y' >>> env['PATH_INFO'] = '/undefined' Or why can't you use? class RalphMod < Hash def []=(index, value) p index p value if index == 'PATH_INFO' and value == '/undefined' puts 'found it!' end super index, value end end env = RalphMod.new() env['X'] = 'Y' env['PATH_INFO'] = '/undefined'