From: Ralph Shnelvar Date: 2010-02-04T12:11:10+09:00 Subject: Re: hooking subscript operations in a hash 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' >> The values are not displayed and 'found it!' is also not displayed. RD> A few problems with this code. RD> First RD> class Hash RD> extend RalphMod RD> end RD> makes the method in RalphMod a method of the Hash class object not RD> hash instances. RD> The way to do that would be RD> class Hash RD> include RalphMod RD> end RD> but this wouldn't work either because methods in included modules come RD> after methods defined in the class, so you'll still invoke the RD> original []= method. RD> What you want is something like this: RD> module RalphMod RD> def []=(index, value) RD> p index RD> p value RD> if index == 'PATH_INFO' and value == '/undefined' RD> puts 'found it!' RD> end RD> super index, value RD> end RD> end RD> env = {} RD> env.extend RalphMod RD> env['X'] = 'Y' RD> env['PATH_INFO'] = '/undefined' RD> This effectively inserts your module between the instance of Hash RD> referenced by the variable env and it's class. Yeah ... tried that ... but apparently env is going in and out of scope. Is there any way to do it for all hash subscript operations?