# How to monkeypatch Hash's \[\]= method

**URL:** <https://www.ruby-forum.com/t/how-to-monkeypatch-hashs-method/157061>\
**Category:** Rails\
**Created:** [January 7, 2009, 12:37am UTC](https://www.ruby-forum.com/t/how-to-monkeypatch-hashs-method/157061 "2009-01-07T00:37:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [January 7, 2009, 12:37am UTC](https://www.ruby-forum.com/t/how-to-monkeypatch-hashs-method/157061/1 "2009-01-07T00:37:41Z")

</div>

I’m trying to monkeypatch the []= method in the Hash class:

Hash.class\_eval do  
def []\_with\_feature=(a,b)  
puts ‘foo’  
end  
alias\_method\_chain :[]=, :feature  
end

Something is wrong with the syntax, but I can’t figure out what. The  
alias\_method\_chain method does look for punctuation (= in this case)  
and expects it to be at the end of the aliased method ([]  
\_with\_feature=), but I get the following syntax error:

SyntaxError: compile error  
(irb):5: syntax error, unexpected ‘)’, expecting ‘=’  
(irb):7: syntax error, unexpected kEND, expecting ‘)’  
(irb):9: syntax error, unexpected kEND, expecting ‘)’

Any ideas?

Thanks,  
-H

---

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [January 7, 2009, 12:49am UTC](https://www.ruby-forum.com/t/how-to-monkeypatch-hashs-method/157061/2 "2009-01-07T00:49:58Z")

</div>

On 6 Jan 2009, at 23:36, Harold wrote:

> I’m trying to monkeypatch the []= method in the Hash class:

doesn’t seem to be anything to do with alias\_method\_chain - just  
sticking  
def []\_with\_feature=(a,b)  
puts ‘foo’  
end

into irb causes similar errors. You’ll just have to skip alias method  
chain this time.

Fred

---

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [January 7, 2009, 12:57am UTC](https://www.ruby-forum.com/t/how-to-monkeypatch-hashs-method/157061/3 "2009-01-07T00:57:01Z")

</div>

Thanks for the response.

So how in the world is this implemented? The []= method’s source code is  
in  
C. Other classes override it in a subclass, which works fine, but how  
would  
I go about implementing a method that works like the Hash class’ []=  
method?

I’m not sure what you mean by “skipping alias\_method\_chain” this time.  
If I  
do

alias :[]\_without\_feature=, :[]=  
alias :[]=, :[]\_with\_feature

I get the same error, as expected. So how would I go about changing this  
method’s behavior?

Thanks again,  
-H

On Tue, Jan 6, 2009 at 6:49 PM, Frederick C.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [January 7, 2009, 1:22am UTC](https://www.ruby-forum.com/t/how-to-monkeypatch-hashs-method/157061/4 "2009-01-07T01:22:12Z")

</div>

On 6 Jan 2009, at 23:56, Harold A. Giménez Ch. wrote:

> alias :[]\_without\_feature=, :[]=  
> alias :[]=, :[]\_with\_feature
> 
> I get the same error, as expected. So how would I go about changing  
> this method’s behavior?

just call it square\_bracket\_without feature or something like that.  
the problem isn;t the method aliasing it’s defining a method called  
[]\_with\_feature=

Fred

---

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [January 7, 2009, 3:39am UTC](https://www.ruby-forum.com/t/how-to-monkeypatch-hashs-method/157061/5 "2009-01-07T03:39:36Z")

</div>

I was focusing on the “=” being the problem… Worked nicely. Many  
thanks!

On Tue, Jan 6, 2009 at 7:20 PM, Frederick C.  
\<removed\_email\_address@domain.invalid
