anonymous classes

I

Ian Macdonald

Hi,

Could someone please explain to me the concept of an anonymous class?

I'm talking about this construct:

class << Foo
...
end

I'm having a hard time understanding the books and figuring out when I
might actually want/need to use this.

Ian
--
Ian Macdonald | Keep cool, but don't freeze. -- Hellman's
System Administrator | Mayonnaise
(e-mail address removed) |
http://www.caliban.org |
|
 
I

Ian Macdonald

What you've written is called a "singleton class". I wrote this a while:

http://www.rubygarden.org/ruby?SingletonTutorial

It might be helpful and includes a couple of links to more detailled
information.

Thanks. This is an excellent resource and makes things a lot clearer.

Ian
--
Ian Macdonald | Yes, we will be going to OSI, Mars, and
System Administrator | Pluto, but not necessarily in that order.
(e-mail address removed) | -- Jeffrey Honig
http://www.caliban.org |
|
 
D

Dominik Werder

What you've written is called a "singleton class". I wrote this a while:


it is very helpful for me too :)

One question referring to
"A neat solution then is to create a singleton class for each POP3
connection as it arrives: "
How much memory does this technique consume? Are the new methods created
for each individual instance? Or just something like a lookup table?

bye!
Dominik
 
B

Brian Candler

it is very helpful for me too :)

One question referring to
"A neat solution then is to create a singleton class for each POP3
connection as it arrives: "
How much memory does this technique consume? Are the new methods created
for each individual instance? Or just something like a lookup table?

All that happens is that a new proxy class is added to the object, which
points at the module which you #extended it with. It certainly doesn't copy
all the methods.

You can prove this easily enough, by modifying the module *after* you have
done extend:

module A
end
a = "hello"
a.extend A
b = "world"
b.extend A

module A
def foo
puts "it works"
end
end
a.foo #=> "it works"
b.foo #=> "it works"

You can see that the two objects share the same module implementation.

Regards,

Brian.
 

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

Similar Threads

detecting default parameters 4
BCC32 under win environment 1
=== confusion 1
overriding setup in Test::Unit 3
Ruby blogs 4
SOAP4R and WSDL 8
purpose of replace method 7
bug in shell.rb 0

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top