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.
> A few problems with this code.
> First
> class Hash
> extend RalphMod
> end
> makes the method in RalphMod a method of the Hash class object not
> hash instances.
> The way to do that would be
> class Hash
> include RalphMod
> end
> but this wouldn't work either because methods in included modules come
> after methods defined in the class, so you'll still invoke the
> original = method.
> What you want is something like this:
> 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
> env = {}
> env.extend RalphMod
> env['X'] = 'Y'
> env['PATH_INFO'] = '/undefined'
> This effectively inserts your module between the instance of Hash
> referenced by the variable env and it's class.
Yeah ... tried that ... but apparently env is going in and out of scope.
Well, that's not a problem. As long as you retain a reference to the object somewhere it will stay there.
Is there any way to do it for all hash subscript operations?
You have been shown solutions but frankly I would not do it. This might have totally unwanted side effects on other code which uses Hash as well. Better not modify core classes and instead either inherit them or - even better - encapsulate them in another class. This has the added advantage that nobody can modify the Hash in unwanted ways and you are exposing only that part of Hash's interface that you intend to.
class RalphHash
def initialize
@h = {}
end
def =(k,v)
if k == 'PATH_INFO' and v == '/undefined'
puts 'found it!'
end
@h[k]=v
end
def (k)
@h[k]
end
end
You can also use delegate for this.
http://ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html
Kind regards
robert
···
On 04.02.2010 04:11, Ralph Shnelvar wrote:
> On Wed, Feb 3, 2010 at 9:38 PM, Ralph Shnelvar <ralphs@dos32.com> wrote:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/