Anonymous classes

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
ian@caliban.org |
http://www.caliban.org |
>

Ian Macdonald wrote:

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

This may be a good starting point:

Chad

I use this construction most when im doing unittesting, in order to access
members I otherwise don’t have permission to access.

def test_macro2
	m = FakeMementoBig.new
	ct = FakeCaretaker.new(m)
	class << ct
		attr_reader :record_mode
	end
	ct.macro_begin
	assert_equal(true, ct.record_mode)
	ct.execute_undo
	assert_equal(false, ct.record_mode)
	assert_exception(FakeCaretaker::Nothing2Redo) { ct.execute_redo }
	assert_exception(FakeCaretaker::Nothing2Undo) { ct.execute_undo }
end 

Another place I use it, is for building menues:

def build_menu_bottom
	menu = [
		"1", "Record",
		" 2", "Play",
		" 3", "Undo",
		" 4", "Redo",
		"   5", "S.Left",
		" 6", "S.Down",
		" 7", "S.Up",
		" 8", "S.Right"
	]
	class << menu
		def slice2
			until empty?
				yield(*slice!(0, 2))
			end
		end
	end
	res = []
	menu.slice2 do |sep, text|
		res += sep.to_cells(Cell::MENU_SEP) +
			text.to_cells(Cell::MENU)
	end
	res
end
···

On Tue, 01 Jul 2003 09:50:31 +0900, Ian Macdonald wrote:

class << Foo

end

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


Simon Strandgaard

What you’ve written is called a “singleton class”. I wrote this a while:

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

Regards,

Brian.

···

On Tue, Jul 01, 2003 at 08:50:31AM +0900, Ian Macdonald wrote:

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.

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

Ian

···

On Tue 01 Jul 2003 at 19:04:23 +0900, Brian Candler wrote:

On Tue, Jul 01, 2003 at 08:50:31AM +0900, Ian Macdonald wrote:

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.

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.


Ian Macdonald | Yes, we will be going to OSI, Mars, and
System Administrator | Pluto, but not necessarily in that order.
ian@caliban.org | – Jeffrey Honig
http://www.caliban.org |
>

What you’ve written is called a “singleton class”. I wrote this a while:

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

it is very helpful for me too :slight_smile:

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

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.

···

On Wed, Jul 02, 2003 at 05:36:21PM +0900, Dominik Werder wrote:

What you’ve written is called a “singleton class”. I wrote this a while:

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

it is very helpful for me too :slight_smile:

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?