<?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>occasionally useful &#187; jruby</title>
	<atom:link href="http://blog.maxaller.name/tag/jruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.maxaller.name</link>
	<description>ruby, ubuntu, etc</description>
	<lastBuildDate>Sun, 11 Jul 2010 07:01:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Destructors in Ruby?  Not quite&#8230;</title>
		<link>http://blog.maxaller.name/2009/12/destructors-in-ruby-not-quite/</link>
		<comments>http://blog.maxaller.name/2009/12/destructors-in-ruby-not-quite/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 00:54:35 +0000</pubDate>
		<dc:creator>Max</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[destructor]]></category>
		<category><![CDATA[jruby]]></category>

		<guid isPermaLink="false">http://blog.maxaller.name/?p=247</guid>
		<description><![CDATA[So I was curious to see how destructors work in Ruby, and...they don't.  Or rather, the method we do have, ObjectSpace.define_finalizer, is rather restrictive.  But it does leave a loophole -- the callback method receives an object_id.

If you're thinking this is a bad idea, you might be right -- this is just a [...]]]></description>
			<content:encoded><![CDATA[<p>So I was curious to see how destructors work in Ruby, and...they don't.  Or rather, the method we do have, ObjectSpace.define_finalizer, is rather restrictive.  But it does leave a loophole -- the callback method receives an object_id.<br />
<span id="more-247"></span><br />
If you're thinking this is a bad idea, you might be right -- this is just a proof of concept showing that it's possible.  Anyway, so the main restriction on define_finalizer callback is it can't refer to the object being freed.  If you try, the callback fails silently (except on JRuby 1.4.0! the exception propagates there).  This is because the callback executes <em>after</em> the object has been freed, so there is no "self".  Here's the proof of concept of one possible solution: maintain a map of object ids to resources: <a href="http://gist.github.com/255174" target="_blank">gist</a>.  The module, sample app, and output from Ruby 1.8(.7), 1.9, and JRuby 1.4.0 are also included.</p>
<p>Edit: apparently you can embed Gists.  Sweet.  Gist is below.</p>
<p><script src="http://gist.github.com/255174.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.maxaller.name/2009/12/destructors-in-ruby-not-quite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
