<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Marco Mastrodonato &#187; ObjectSpace</title>
	<atom:link href="http://mastrodonato.info/index.php/tag/objectspace/feed/" rel="self" type="application/rss+xml" />
	<link>http://mastrodonato.info</link>
	<description>Non c&#039;e&#039; prezzo per la miticita&#039;</description>
	<lastBuildDate>Mon, 06 Sep 2010 21:05:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Distruttori di classe #3</title>
		<link>http://mastrodonato.info/index.php/2009/06/distruttori-di-classe-3/</link>
		<comments>http://mastrodonato.info/index.php/2009/06/distruttori-di-classe-3/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 20:29:02 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Distruttori]]></category>
		<category><![CDATA[garbage collector]]></category>
		<category><![CDATA[ObjectSpace]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=25</guid>
		<description><![CDATA[Eccoci all&#8217;ultimo articolo della serie rivolta ai distruttori di classe e più in generale a come fare per inserire un metodo da richiamare alla fine dell&#8217;utilizzo di una classe. Nel precedente articolo abbiamo visto come fare per generare un evento in fase di distruzione dell&#8217;oggetto da parte del garbage collector e che richiami un metodo [...]]]></description>
			<content:encoded><![CDATA[<p>Eccoci all&#8217;ultimo articolo della serie rivolta ai distruttori di classe e più in generale a come fare per inserire un metodo da richiamare alla fine dell&#8217;utilizzo di una classe.</p>
<p>Nel precedente articolo abbiamo visto come fare per generare un evento in fase di distruzione dell&#8217;oggetto da parte del garbage collector e che richiami un metodo per svolgere le ultimissime operazioni.</p>
<p>Passiamo alla pratica e dopo i commenti:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> E
  <span style="color:#008000; font-style:italic;">#Creo una variabile di istanza, utile per i test</span>
  attr_reader <span style="color:#ff3333; font-weight:bold;">:var1</span>
  @@count=<span style="color:#006666;">0</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#Costruttore</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>var1<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Initializing #{self.object_id} var #{var1}&quot;</span>
    <span style="color:#CC00FF; font-weight:bold;">ObjectSpace</span>.<span style="color:#9900CC;">define_finalizer</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>, <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:finalize</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_proc</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@var1</span>=var1
    @@count <span style="color:#006600; font-weight:bold;">+</span>=<span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#Metodo di servizio</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> body
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">StandardError</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> err
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Error: #{err}&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">ensure</span>
      finalize
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#Metodo che fa il lavoro sporco</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> work
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'Work'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  private
  <span style="color:#008000; font-style:italic;">#Distruttore</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> finalize
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Finalize #{@var1}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">finalize</span><span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;*** Finalize id #{id} nr. #{@@count} ***&quot;</span>
    @@count <span style="color:#006600; font-weight:bold;">-</span>=<span style="color:#006666;">1</span>
    <span style="color:#008000; font-style:italic;">#puts &quot;Var1 #{ObjectSpace._id2ref(id).var1}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Istanzio tre oggetti e li utilizzo</span>
<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'aaa'</span>,<span style="color:#996600;">'bbb'</span>,<span style="color:#996600;">'ccc'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>str<span style="color:#006600; font-weight:bold;">|</span>
  E.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>str<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">body</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>o<span style="color:#006600; font-weight:bold;">|</span> 
    o.<span style="color:#9900CC;">work</span>
    <span style="color:#008000; font-style:italic;">#...e tutti gli altri metodi</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>L&#8217;output generato sarà questo:</p>
<p><code><br />
D:\Marco\Progetti\Ruby\Batch\Test\Destructor>ruby D3.rb<br />
Initializing 20806360 var aaa<br />
Work<br />
Finalize aaa<br />
Initializing 20806220 var bbb<br />
Work<br />
Finalize bbb<br />
Initializing 20806080 var ccc<br />
Work<br />
Finalize ccc<br />
*** Finalize id 20806080 nr. 3 ***<br />
*** Finalize id 20806220 nr. 2 ***<br />
*** Finalize id 20806360 nr. 1 ***<br />
</code></p>
<p>Ci sono un pò di differenze rispetto al precedente articolo:</p>
<p>Questa volta il distruttore è richiamato direttamente nel costruttore perchè ho ipotizzato che servisse ad ogni oggetto, l&#8217;alternativa è definirlo come in un esempio precedente, solo per gli oggetti interessati, dopo l&#8217;istanziamento:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">E.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'xxx'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">body</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>o<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC00FF; font-weight:bold;">ObjectSpace</span>.<span style="color:#9900CC;">define_finalizer</span><span style="color:#006600; font-weight:bold;">&#40;</span>o, o.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:finalize</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_proc</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  o.<span style="color:#9900CC;">work</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Un altra differenza è che questa volta non si associa una procedura creata &#8220;al volo&#8221; ma un metodo di classe, nella definizione della classe è infatti presente un nuovo metodo: self.finalize</p>
<p>Ho inserito una variabile di classe, chiamata anche attributo statico, @@count. E&#8217; in comune con tutte le istanze e nell&#8217;esempio serve per conteggiare gli oggetti della classe E. Nel distruttore viene mostrato il valore per informarci del numero rimanente di oggetti.</p>
<p>Questa volta non è necessario forzare l&#8217;esecuzione del garbage collector in quanto al termine dello script viene fatto in automatico generando l&#8217;evento che richiama il costruttore per ogni oggetto.</p>
<p><em>Ho cercato di semplificare il codice e cosi facendo ho omesso una finezza di ruby che devo però indicarla almeno come nota anche se non c&#8217;entra nulla coi distruttori. Se i metodi da richiamare sono molti si può utilizzare questa sintassi lasciando il codice molto leggibile:</em></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">E.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'xxx'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">body</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>o<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC00FF; font-weight:bold;">ObjectSpace</span>.<span style="color:#9900CC;">define_finalizer</span><span style="color:#006600; font-weight:bold;">&#40;</span>o, o.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:finalize</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_proc</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'work'</span>, <span style="color:#996600;">'hardwork'</span>, <span style="color:#996600;">'ecc'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>method_string<span style="color:#006600; font-weight:bold;">|</span>
        o.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>method_string<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Purtroppo non riesco ad utilizzare un metodo distruttore che sia univoco per istanza. Un altro tentativo poteva essere quello di puntare all&#8217;istanza tramite ObjectSpace che tramite il metodo _id2ref restituisce l&#8217;oggetto con id uguale a quello passato come parametro:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">ObjectSpace</span>._id2ref<span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Sfortunatamente non funziona dall&#8217;interno del distruttore, probabilmente il garbage collector ha già distrutto l&#8217;istanza, questo è un peccato e spero esista un altro sistema che però non conosco&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/06/distruttori-di-classe-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Distruttori di classe #2</title>
		<link>http://mastrodonato.info/index.php/2009/06/distruttori-di-classe-2/</link>
		<comments>http://mastrodonato.info/index.php/2009/06/distruttori-di-classe-2/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 20:28:18 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Distruttori]]></category>
		<category><![CDATA[garbage collector]]></category>
		<category><![CDATA[ObjectSpace]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=18</guid>
		<description><![CDATA[Nell&#8217;articolo precedente abbiamo visto come creare un metodo che permetta di rilasciare le risorse al termine dell&#8217;utilizzo. Non sempre però è possibile determinare il momento del rilascio, ad esempio quando è legato ad eventi esterni. In questo caso è necessario che il distruttore sia eseguito proprio quando l&#8217;oggetto sia in fase di smaltimento, in automatico. [...]]]></description>
			<content:encoded><![CDATA[<p>Nell&#8217;articolo precedente abbiamo visto come creare un metodo che permetta di rilasciare le risorse al termine dell&#8217;utilizzo. Non sempre però è possibile determinare il momento del rilascio, ad esempio quando è legato ad eventi esterni. In questo caso è necessario che il distruttore sia eseguito proprio quando l&#8217;oggetto sia in fase di smaltimento, in automatico.</p>
<p>Con un linguaggio che adopera un garbage collector però bisogna fare molta attenzione per il semplice motivo che non si sa quando verrà eseguita la pulizia e solo in questo momento viene scaturito l&#8217;evento che richiama il metodo distruttore.</p>
<p>Quando distruggiamo esplicitamente un oggetto da codice, valorizzandolo con nil, stiamo solamente indicando l&#8217;intenzione, sarà poi compito del garbage collector analizzare se effettivamente non esiste nessun riferimento a quell&#8217;area di memoria, basta un solo indirizzamento anche involontario (i casi più probabili) per impedirne il rilascio. Poi c&#8217;è l&#8217;incognita di quando tutto ciò verrà fatto.</p>
<p>Riprendiamo l&#8217;esempio precedente con qualche aggiunta al codice:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> E
  <span style="color:#008000; font-style:italic;">#Creo una variabile di istanza, utile per i test</span>
  attr_reader <span style="color:#ff3333; font-weight:bold;">:var1</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#Costruttore</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>var1<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Initializing #{self.object_id} var #{var1}&quot;</span>
    <span style="color:#0066ff; font-weight:bold;">@var1</span>=var1
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#Metodo di servizio</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> body
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      <span style="color:#9966CC; font-weight:bold;">yield</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">StandardError</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> err
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Error: #{err}&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">ensure</span>
      finalize
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#Metodo che fa il lavoro sporco</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> work
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'Work'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  private
  <span style="color:#008000; font-style:italic;">#Distruttore</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> finalize
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;End #{@var1}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Istanzio il primo oggetto</span>
e1=E.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'Try1'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
e1.<span style="color:#9900CC;">body</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  e1.<span style="color:#9900CC;">work</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Istanzio il secondo oggetto per il quale ho anche la necessità</span>
<span style="color:#008000; font-style:italic;">#di rilasciare alcune risorse in un momento indeterminato</span>
e2=E.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'Try2'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
e2.<span style="color:#9900CC;">body</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#CC00FF; font-weight:bold;">ObjectSpace</span>.<span style="color:#9900CC;">define_finalizer</span><span style="color:#006600; font-weight:bold;">&#40;</span>e2, <span style="color:#CC0066; font-weight:bold;">proc</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>id<span style="color:#006600; font-weight:bold;">|</span>puts <span style="color:#996600;">&quot;Finalizing #{id}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  e2.<span style="color:#9900CC;">work</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'Oggetti prima della pulizia:'</span>
<span style="color:#CC00FF; font-weight:bold;">ObjectSpace</span>.<span style="color:#9900CC;">each_object</span><span style="color:#006600; font-weight:bold;">&#40;</span>E<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>o<span style="color:#006600; font-weight:bold;">|</span>puts <span style="color:#996600;">&quot;Oggetto: #{o.object_id}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Distruggo gli oggetti</span>
e1=<span style="color:#0000FF; font-weight:bold;">nil</span>
e2=<span style="color:#0000FF; font-weight:bold;">nil</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'Richiamo il garbage collector per la pulizia...'</span>
<span style="color:#CC00FF; font-weight:bold;">GC</span>.<span style="color:#9900CC;">start</span>
<span style="color:#CC0066; font-weight:bold;">sleep</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'Oggetti dopo la pulizia:'</span>
<span style="color:#CC00FF; font-weight:bold;">ObjectSpace</span>.<span style="color:#9900CC;">each_object</span><span style="color:#006600; font-weight:bold;">&#40;</span>E<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>o<span style="color:#006600; font-weight:bold;">|</span>puts <span style="color:#996600;">&quot;Oggetto: #{o.object_id}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Se eseguiamo questo script otterremo il seguente output:<br />
<code><br />
D:\Marco\Progetti\Ruby\Batch\Test>ruby D2.rb<br />
Initializing 20805980 var Try1<br />
Work<br />
End Try1<br />
Initializing 20805910 var Try2<br />
Work<br />
End Try2<br />
Oggetti prima della pulizia:<br />
Oggetto: 20805910<br />
Oggetto: 20805980<br />
Richiamo il garbage collector per la pulizia...<br />
Finalizing 20805910<br />
Oggetti dopo la pulizia:<br />
</code></p>
<p>La definizione della classe è rimasta identica, questa volta stanzio due oggetti, il primo è identico all&#8217;articolo precedente, per il secondo invece ho ipotizzato che dovesse gestire il rilascio di alcune risorse in un momento x, noi non sappiamo quando si verificherà sappiamo solo che quando avverrà dobbiamo fare in modo che vengano deallocate alcune risorse.</p>
<p>Un esempio concreto è quando si scrive un file mediante un processo esterno, dobbiamo riferire di chiuderlo prima di terminare la nostra applicazione, anche se si verifica qualcosa di inatteso.</p>
<p>Se esaminiamo il secondo oggetto:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">e2=E.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'Try2'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
e2.<span style="color:#9900CC;">body</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#CC00FF; font-weight:bold;">ObjectSpace</span>.<span style="color:#9900CC;">define_finalizer</span><span style="color:#006600; font-weight:bold;">&#40;</span>e2, <span style="color:#CC0066; font-weight:bold;">proc</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>id<span style="color:#006600; font-weight:bold;">|</span>puts <span style="color:#996600;">&quot;Finalizing #{id}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  e2.<span style="color:#9900CC;">work</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Si nota che abbiamo fatto uso della classe ObjectSpace, contenitore di tutte le classi stanziate, per associare la dimissione dell&#8217;oggetto e2 alla proc passata come parametro. Tradotto in parole semplici stiamo dicendo: prima di distruggere l&#8217;oggetto esegui quella procedura.</p>
<p>Se riguardiamo l&#8217;output si può notare un altro l&#8217;utilizzo della classe ObjectSpace:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">ObjectSpace</span>.<span style="color:#9900CC;">each_object</span><span style="color:#006600; font-weight:bold;">&#40;</span>E<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>o<span style="color:#006600; font-weight:bold;">|</span>puts <span style="color:#996600;">&quot;Oggetto: #{o.object_id}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>E&#8217; possibile esaminare ogni oggetto istanziato e specificando come parametro la classe E possiamo filtrare solo quella che ci interessa (attenzione che senza filtro la lista potrebbe essere molto lunga).</p>
<p>Tornando all&#8217;output: gli oggetti trovati nel contenitore sono due e viene mostrato l&#8217;id di riferimento. Dopo di che vengono distrutti e viene forzata l&#8217;esecuzione del garbage collector con GC.start. In alternativa a questo è anche possibile eseguire il metodo garbage_collect di ObjectSpace</p>
<p>Dopo la pulizia viene generato l&#8217;evento che richiama la proc associata all&#8217;oggetto, quella che nell&#8217;esempio scrive a video: Finalizing 20805910. L&#8217;id dell&#8217;oggetto è passato come parametro al blocco e noi lo mostriamo. Se andiamo a riesaminare il contenitore degli oggetti questa volta non conterrà nessuna istanza della classe E.</p>
<p>Nel prossimo articolo esaminerò l&#8217;utilizzo di metodi distruttori da associare all&#8217;evento di distruzione dell&#8217;oggetto. Tratterò anche la problematica legata alle variabili di istanza.</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/06/distruttori-di-classe-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
