Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Subclassing in module from top module?

Reply
Thread Tools

Subclassing in module from top module?

 
 
Vít Ondruch
Guest
Posts: n/a
 
      10-12-2009
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      10-12-2009
El Lunes, 12 de Octubre de 2009, V=C3=ADt Ondruch escribi=C3=B3:
> irb(main):017:1> class Hash << ::Hash


Wrong, use:

class Hash < ::Hash


=2D-=20
I=C3=B1aki Baz Castillo <(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
Aaron Patterson
Guest
Posts: n/a
 
      10-12-2009
On Oct 12, 2009, at 9:20 AM, V=EDt 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 =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

---
Aaron Patterson
http://tenderlovemaking.com





 
Reply With Quote
 
Vít Ondruch
Guest
Posts: n/a
 
      10-12-2009
Iñaki Baz Castillo 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
subclassing a module: misleading(?) error message Erik Johnson Python 4 01-11-2007 06:50 PM
"from module import *" and modifying module's top-level vars lemke_juergen@yahoo.de Python 1 10-29-2006 08:55 PM
VoIPCheap/Stunt/SIPDiscount/Et.al - Mobile - Top-up Expiry Period -- Campaign for Correct Expiry Period on Finarea VOIP Service Mobile Top-Ups News Reader UK VOIP 16 06-26-2006 05:03 PM
Top-Up Methods - Finarea (Voipcheap, internetcalls, etc.) et. al., VOIP Services Question - Top-up Methods News Reader UK VOIP 0 04-10-2006 02:41 PM
Why does my Top DashBoard Image do not touch top of the screen? Frederic HOUDE HTML 4 08-30-2004 11:22 PM



Advertisments