<?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>Live &#38; Code &#187; mystery</title>
	<atom:link href="http://www.liveandcode.com/tag/mystery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.liveandcode.com</link>
	<description>Enrico on programming, living, and everything in between</description>
	<lastBuildDate>Fri, 07 May 2010 13:00:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Adventures in Ruby: When Constants Aren&#8217;t</title>
		<link>http://www.liveandcode.com/2009/11/16/adventures-in-ruby-when-constants-arent/</link>
		<comments>http://www.liveandcode.com/2009/11/16/adventures-in-ruby-when-constants-arent/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 18:14:48 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[mystery]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=249</guid>
		<description><![CDATA[I just squashed a bug that had me scratching my head for at least a good half-hour or so involving a class constant that kept on getting changed. Here&#8217;s the setup (anonymized so that I&#8217;m not exposing gooey proprietary secrets): class WebTransaction &#160; # Base URL for transaction web service SERVICE_URL = &#34;http://accountingservice.com/&#34; &#160; def [...]]]></description>
			<content:encoded><![CDATA[<p>I just squashed a bug that had me scratching my head for at least a good half-hour or so involving a class constant that kept on getting changed. Here&#8217;s the setup (anonymized so that I&#8217;m not exposing gooey proprietary secrets):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> WebTransaction
&nbsp;
  <span style="color:#008000; font-style:italic;"># Base URL for transaction web service</span>
  SERVICE_URL = <span style="color:#996600;">&quot;http://accountingservice.com/&quot;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> transaction_url
    url = SERVICE_URL
    url <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;VAL1=foo&quot;</span>
    url <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;&amp;VAL2=bar&quot;</span>
    url
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The class is meant to represent a transaction being posted against a rather odd web service that actually uses GET rather than POST for posting transactions (bad web service!). In my actual code, the URL parameters appended to the SERVICE_URL base would be determined on a per-object basis but I&#8217;ve simplified it here.</p>
<p>Here&#8217;s the punchline: <code>SERVICE_URL</code> was <em>changing!</em> Calls to <code>transaction_url</code> would keep on appending more variables to it. If you&#8217;re particularly clever with Ruby, you&#8217;ve already figured out exactly why. But if you&#8217;re scratching your head like I was, here are some hints:</p>
<ul>
<li>In Ruby, constants <em>aren&#8217;t</em>. In fact, they&#8217;re really no different from variables except for the fact that Ruby detects that variable names in all-caps are probably supposed to stay constant and warns if you try to assign to them.</li>
<li><code>&lt;&lt;</code>, for strings, will append to the end of the string.</li>
<li>Ruby strings are mutable. That is, operations on a string variable will usually be done in place, rather than returning a new string. (Method calls, on the other hand, are a different story!)</li>
<li>Assigning a String variable to another String variable will assign the reference. That is, the two variables will be pointing at the same object.</li>
</ul>
<p>Hit the jump to see the solution to this little mystery&#8230;<br />
<span id="more-249"></span></p>
<p><code>SERVICE_URL</code> is only directly assigned to when it is declared. But what happens when I assign <code>SERVICE_URL</code> to the local variable in <code>transaction_url</code>, <code>url</code>. <em>They&#8217;re pointing to the same object!</em></p>
<p>Then I go ahead and I start appending things to the end of the string. Ruby has no complaints because I&#8217;m not assigning to <code>SERVICE_URL</code> but <em>the object that it is pointing to is being changed!</em> The operation is causing the URL parameters to be appended in place.</p>
<p>And that, friends, is what happens when constants aren&#8217;t. Let&#8217;s fix this little error:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> WebTransaction
&nbsp;
  <span style="color:#008000; font-style:italic;"># Base URL for transaction web service</span>
  SERVICE_URL = <span style="color:#996600;">&quot;http://accountingservice.com/&quot;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> transaction_url
    url = SERVICE_URL.<span style="color:#9900CC;">clone</span>
    url <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;VAL1=foo&quot;</span>
    url <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;&amp;VAL2=bar&quot;</span>
    url
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now, instead of appending to the string object referenced by <code>SERVICE_URL</code> directly, I&#8217;m creating a copy of the object and then appending URL parameters to that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2009/11/16/adventures-in-ruby-when-constants-arent/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Where is why?</title>
		<link>http://www.liveandcode.com/2009/08/19/where-is-why/</link>
		<comments>http://www.liveandcode.com/2009/08/19/where-is-why/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 21:11:07 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[mystery]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[_why]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=185</guid>
		<description><![CDATA[&#8220;Why The Lucky Stiff&#8221;, one of the most influential characters in the Ruby community, has simply vanished. His Twitter account, GitHub account, and most of his websites are gone without a trace. Why The Lucky Stiff&#8217;s contributions to the Ruby community include &#8220;Why&#8217;s Poignant Guide to Ruby&#8221;, a book which many state was the reason [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;Why The Lucky Stiff&#8221;, one of the most influential characters in the Ruby community, has simply vanished. His <a href="http://twitter.com/_why">Twitter account</a>, <a title="why's Profile - GitHub" href="http://github.com/why/">GitHub account</a>, and <a title="Discussion on Hacker News about _why's disappearance" href="http://news.ycombinator.com/item?id=773108">most of his websites</a> are gone without a trace.</p>
<p>Why The Lucky Stiff&#8217;s contributions to the Ruby community include &#8220;Why&#8217;s Poignant Guide to Ruby&#8221;, a book which many state was the reason they got into Ruby, Shoes, an easy-to-use cross-platform GUI toolkit with innovative online distribution features, and Hpricot, a very slick HTML parser that is also a joy to use.</p>
<p>Some believe that he has decided to move on from software development because his last tweet reads &#8220;programming is rather thankless. u see your works become replaced by superior ones in a year. unable to run at all in a few more.&#8221; Some believe that his accounts were all hacked. A few others believe that his anonymity has been <a title="Who is why the lucky stiff?" href="http://whoiswhytheluckystiff.wordpress.com/">compromised</a> and that he has decided the destroy the pseudonym.</p>
<p>Wherever Why The Lucky Stiff goes from here, I hope he knows that there are so many of us who looked up to him and have him to thank for knowing the joy that is programming in Ruby. He will be sorely missed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2009/08/19/where-is-why/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
