<?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>threehv &#187; constructors</title>
	<atom:link href="http://www.3hv.co.uk/blog/tag/constructors/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.3hv.co.uk/blog</link>
	<description>precision engineering for your website</description>
	<lastBuildDate>Fri, 13 Jan 2012 16:18:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Constructors in Ruby are not guaranteed to be called</title>
		<link>http://www.3hv.co.uk/blog/2009/06/03/constructors-in-ruby-are-not-guaranteed-to-be-called/</link>
		<comments>http://www.3hv.co.uk/blog/2009/06/03/constructors-in-ruby-are-not-guaranteed-to-be-called/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 21:28:58 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[constructors]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=557</guid>
		<description><![CDATA[Today, Caius made a discovery that shocked me. He had a class, descending from ActiveRecord::Base, with a custom constructor (initialize method). To debug it, he had the constructor raise an exception. In the console, Thingy.new(params) raised the exception as expected. But wotsit.thingies.find_by_field(value) did not. Even though it was instantiating an instance of Thingy and returning [...]]]></description>
			<content:encoded><![CDATA[<p>Today, <a href="http://caius.name/">Caius</a> made a discovery that shocked me.  </p>
<p>He had a class, descending from ActiveRecord::Base, with a custom constructor (initialize method).  To debug it, he had the constructor raise an exception.  In the console, <code>Thingy.new(params)</code> raised the exception as expected.  But <code>wotsit.thingies.find_by_field(value)</code> did not.  Even though it was instantiating an instance of Thingy and returning it.  </p>
<p>&#8220;It must not be calling the constructor&#8221; he said.<br />
&#8220;Rubbish&#8221; said I, &#8220;it&#8217;s a constructor.  Constructors are always called.  That&#8217;s the point of them&#8221;.  </p>
<p>But as he dug deeper it certainly looked like the constructor wasn&#8217;t being called.  </p>
<p>And then he found an article explaining that you should <a href="http://blog.dalethatcher.com/2008/03/rails-dont-override-initialize-on.html">never rely</a> on things being set up on in an Active Record constructor.  Mainly because Active Record uses <code>allocate</code> to instantiate associated objects.  And what is this mysterious allocate?  Why <a href="http://whytheluckystiff.net/articles/rubyOneEightOh.html">explains it all</a>.  </p>
<p>To be honest, I&#8217;ve got mixed feelings about this.  I can see the use of &#8220;allocate&#8221; &#8211; why&#8217;s example of marshalling an object makes sense (I&#8217;m slightly less sure about the way that Active Record uses it to load associations).  But, to my mind, the definition of a constructor is &#8220;the code that is always called when an object is created&#8221;.  So maybe I should just stop thinking of <code>initialize</code> as a constructor and more as an initialiser.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/06/03/constructors-in-ruby-are-not-guaranteed-to-be-called/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.178 seconds -->

