The class's initialize and finalize instance methods will be called when the object is created and when it goes out of scope, respectively. Including this module will make the class's "new" method private, and will add a new class method called "create" that can be used to create instances of the object. A call to create will yield a weak reference to the object.
Note that the object itself may not actually be destroyed until the garbage collector is invoked. The user can keep a reference to the object after the block exits by calling __getobj__ on the reference from inside the block and returning it, but this is not a recommended practice, since the object will be in a half-dead state.
class Foo include AutomaticObject def initialize; puts "initialize"; end def finalize; puts "finalize"; end end
Foo.create do |f| #=> initialize # ... end #=> finalize