Subclassing in module from top module?

V

Vít Ondruch

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 `<main>'



Cheers,

Vit
 
A

Aaron Patterson

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 =20=

though.
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 =20
'<' or
';' or '\n'
class Hash << ::Hash
^
from c:/Ruby/bin/irb:12:in `<main>'

Use one "<" to subclass Hash:

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

Vít Ondruch

Iñaki Baz Castillo said:
El Lunes, 12 de Octubre de 2009, Vít Ondruch escribió:

Wrong, use:

class Hash < ::Hash

Omg, sorry for being stupid and thank you! :)

Vit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top