Subclassing in module from top module?

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

El Lunes, 12 de Octubre de 2009, Vít Ondruch escribió:

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

Wrong, use:

class Hash < ::Hash

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

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! :slight_smile:

Vit

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs