# Subclassing in module from top module?

**URL:** <https://www.ruby-forum.com/t/subclassing-in-module-from-top-module/177161>\
**Category:** Ruby\
**Created:** [October 12, 2009, 6:20pm UTC](https://www.ruby-forum.com/t/subclassing-in-module-from-top-module/177161 "2009-10-12T18:20:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vo\_x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/919ad9/32.png) [@vo\_x](https://www.ruby-forum.com/u/vo_x)\
**Post date:** [October 12, 2009, 6:20pm UTC](https://www.ruby-forum.com/t/subclassing-in-module-from-top-module/177161/1 "2009-10-12T18:20:43Z")

</div>

Hello everybody,

I would like to have locally monkeypatched Hash in my module. I would  
expect that following example will work, however it doesn’t. How to do  
it correctly?

irb(main):016:0\> module Foo  
irb(main):017:1\> class Hash \<\< ::Hash  
irb(main):018:2\> def bar  
irb(main):019:3\> puts ‘bar’  
irb(main):020:3\> end  
irb(main):021:2\> end  
irb(main):022:1\> end  
SyntaxError: (irb):17: syntax error, unexpected tLSHFT, expecting ‘\<’ or  
‘;’ or ‘\n’  
class Hash \<\< ::Hash  
^  
from c:/Ruby/bin/irb:12:in `’

Cheers,

Vit

---

<div class="post-metadata">

**Author:** ![vo\_x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/919ad9/32.png) [@vo\_x](https://www.ruby-forum.com/u/vo_x)\
**Post date:** [October 12, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/subclassing-in-module-from-top-module/177161/2 "2009-10-12T18:33:40Z")

</div>

El Lunes, 12 de Octubre de 2009, VÃ­t Ondruch escribiÃ³:

> irb(main):017:1\> class Hash \<\< ::Hash

Wrong, use:

class Hash \< ::Hash

---

<div class="post-metadata">

**Author:** ![vo\_x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/919ad9/32.png) [@vo\_x](https://www.ruby-forum.com/u/vo_x)\
**Post date:** [October 12, 2009, 6:34pm UTC](https://www.ruby-forum.com/t/subclassing-in-module-from-top-module/177161/3 "2009-10-12T18:34:51Z")

</div>

On Oct 12, 2009, at 9:20 AM, Vít Ondruch wrote:

> Hello everybody,
> 
> I would like to have locally monkeypatched Hash in my module. I would  
> expect that following example will work, however it doesn’t. How to do  
> it correctly?

“local” and “monkeypatch” don’t go together. You can have a subclass  
though.

> class Hash \<\< ::Hash  
> ^  
> from c:/Ruby/bin/irb:12:in `’

Use one “\<” to subclass Hash:

```
 module Foo
   class Hash < ::Hash
     ...
   end
 end

```

* * *

Aaron P.  
[http://tenderlovemaking.com](http://tenderlovemaking.com)

---

<div class="post-metadata">

**Author:** ![vo\_x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/919ad9/32.png) [@vo\_x](https://www.ruby-forum.com/u/vo_x)\
**Post date:** [October 12, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/subclassing-in-module-from-top-module/177161/4 "2009-10-12T19:32:19Z")

</div>

IÃ±aki Baz C. wrote:

> El Lunes, 12 de Octubre de 2009, VÃ­t Ondruch escribiÃ³:
> 
> > irb(main):017:1\> class Hash \<\< ::Hash
> 
> Wrong, use:
> 
> class Hash \< ::Hash

Omg, sorry for being stupid and thank you! 🙂

Vit
