From: Rick DeNatale Date: 2010-02-04T23:08:22+09:00 Subject: Re: hooking subscript operations in a hash On Thu, Feb 4, 2010 at 6:21 AM, Ralph Shnelvar wrote: > Wednesday, February 3, 2010, 8:53:49 PM, you wrote: > > WH> On 2/3/2010 8:11 PM, Ralph Shnelvar wrote: >>> Is there any way to do it for all hash subscript operations? > > irb(main):012:0>> class Hash > irb(main):013:1>> alias_method :old, :[]= > irb(main):014:1>> def []=(index, value) > irb(main):015:2>> p index > irb(main):016:2>> p value > irb(main):017:2>> old index, value > irb(main):018:2>> end > irb(main):019:1>> end > =>> nil > irb(main):020:0>> x={} > =>> {} > irb(main):021:0>> x[:foo]=:bar > WH> :foo > WH> :bar > =>> :bar > > So I tested this in irb and it seems to work fine ... > > Then I stuck this into init.rb as part of a Rails app and ... boom ... > > > f:\Ralph-Rails-Apps\WebOfTrust>ruby script/server webrick --debugger > => Booting WEBrick > => Rails 2.3.5 application starting on http://0.0.0.0:3000 > F:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/validates_captcha-0.9.6/rail > s/init.rb:11:in `alias_method': undefined method `[]=' for class `Rails::Plugin: > :Hash' (NameError) >        from F:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/validates_captc > ha-0.9.6/rails/init.rb:11:in `evaluate_init_rb' >        from F:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib > /rails/plugin.rb:158:in `evaluate_init_rb' I'm guessing a bit here, but I suspect that you inserted this code into the init.rb of the validates_captcha plugin. This is being evaluated as source from the rails plugin initialization code, and the name space is Rails:Plugin So the unscoped name Hash is not the normal and already created Hash class and class Hash .... end creates a new class called Rails::Plugin::Hash with no relationship to ::Hash Now changing that code to class ::Hash .... would get you around this immediate problem, but I'm not sure you really want to do this. Your Rails app is going to spew that debugging information for EVERY instance of Hash or one of it's subclasses. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale