<?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>TJ SingletonRuby -TJ Singleton </title>
	<atom:link href="http://tjsingleton.name/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://tjsingleton.name</link>
	<description>Software Engineer, Baptist Preacher</description>
	<lastBuildDate>Mon, 19 Dec 2011 16:03:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Regex Result Access Benchmark</title>
		<link>http://tjsingleton.name/2010/07/regex-result-access-benchmark/</link>
		<comments>http://tjsingleton.name/2010/07/regex-result-access-benchmark/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 08:35:32 +0000</pubDate>
		<dc:creator>TJ Singleton</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://tjsingleton.name/?p=310</guid>
		<description><![CDATA[The question came on forrst.com about which of the following two styles of accessing the results of a regex match were preferred: &#34;qqq100601.txt&#34;&#91;/\A&#40;&#91;a-z&#93;+&#41;/, 1&#93; &#34;qqq100601.txt&#34;.match&#40;/\A&#40;&#91;a-z&#93;+&#41;/&#41;&#91;0&#93; So I benchmarked it and was surprised that there was such a difference in the performance. Except on jruby, the array style access is the clear winner. Benchmark and Raw [...]]]></description>
			<content:encoded><![CDATA[<p>The question came on <a href="http://forrst.com">forrst.com</a> about which of the following two styles of accessing the results of a regex match were preferred:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#996600;">&quot;qqq100601.txt&quot;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">/</span>\A<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">-</span>z<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#996600;">&quot;qqq100601.txt&quot;</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\A<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">-</span>z<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>So I benchmarked it and was surprised that there was such a difference in the performance. Except on jruby, the array style access is the clear winner.</p>
<p><a href="http://tjsingleton.name/wp-content/uploads/2010/07/Screen-shot-2010-07-01-at-4.23.46-AM.png"><img class="alignnone size-full wp-image-311" title="Graph of Results" src="http://tjsingleton.name/wp-content/uploads/2010/07/Screen-shot-2010-07-01-at-4.23.46-AM.png" alt="" width="545" height="408" /></a></p>
<p><a href="http://gist.github.com/459719">Benchmark and Raw Results</a></p>
<p><strong id="update">Update</strong> I went ahead and ran this through <a href="http://gist.github.com/459719#file_3_ruby_prof.txt">RubyProf</a> in 1.8.7. It turns out that #[/REGEXP/] is optimized to one method call and doesn't instantiate the MatchData object. String#match is delegated to Regexp which instantiates MatchData and then accesses the result for a total of 3 method calls. So the real savings is less object churn and method calls. </p>
]]></content:encoded>
			<wfw:commentRss>http://tjsingleton.name/2010/07/regex-result-access-benchmark/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Speeding up the Haversine Formula in Ruby</title>
		<link>http://tjsingleton.name/2010/06/haversine-formula-in-ruby/</link>
		<comments>http://tjsingleton.name/2010/06/haversine-formula-in-ruby/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 04:49:56 +0000</pubDate>
		<dc:creator>TJ Singleton</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://tjsingleton.name/?p=266</guid>
		<description><![CDATA[A friend of mine's work deals a lot with geographical data. He was working on clustering large sets of coordinates into .25 mile groups and sent me over a small sampling to play with. By small, I mean a 8,736 coordinates. His real data set is much, much larger. He had started off by utilizing [...]]]></description>
			<content:encoded><![CDATA[<p>A friend of mine's work deals a lot with geographical data. He was working on clustering large sets of coordinates into .25 mile groups and sent me over a small sampling to play with. By small, I mean a 8,736 coordinates. His real data set is much, much larger.</p>
<p>He had started off by utilizing <a href="http://geokit.rubyforge.org/">GeoKit</a>, but I noticed that the only thing he was using it for was to calculate distance. His was nicely encapsulated, but this is basically what it was doing.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">point_a = <span style="color:#6666ff; font-weight:bold;">Geokit::LatLng</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">39.416454</span>,<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">118.841204</span><span style="color:#006600; font-weight:bold;">&#41;</span>
point_b = <span style="color:#6666ff; font-weight:bold;">Geokit::LatLng</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">39.476181</span>,<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">118.783931</span><span style="color:#006600; font-weight:bold;">&#41;</span>
point_a.<span style="color:#9900CC;">distance_to</span><span style="color:#006600; font-weight:bold;">&#40;</span>point_b, <span style="color:#ff3333; font-weight:bold;">:units</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:miles</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>I assumed there was some overhead involved in the library, so I wrote <a href="http://github.com/tjsingleton/haversine_benchmark/blob/master/ruby_haversine.rb">my own implementation</a> of the <a href="http://en.wikipedia.org/wiki/Haversine_formula">haversine formula</a>. I did see a slight performance increase, but not much. Geokit seemed to do a pretty good job at staying efficient. </p>
<p>Enter RubyInline. <a href="http://www.zenspider.com/ZSS/Products/RubyInline/">RubyInline</a> is a gem that allows you to write other languages inside your ruby code. It comes with out of the box support for C/C++. I had been wanting to play with RubyInline for awhile, but nothing had come across my plate that needed that level of optimization.</p>
<p>I translated the haversine formula to <a href="http://github.com/tjsingleton/haversine_benchmark/blob/master/inline_haversine.rb">C++ using RubyInline</a> and the results were amazing.  On my machine I was able to reduce the time it took to calculate 1,000,000 distances from 11.247408 seconds to 0.679327 of a second. It only took me a few minutes to rewrite the method  and from an API standpoint it is identical to the pure ruby version. </p>
<p>The next time you're doing work that is easily optimized with C, like this math formula, give a shot and be reap the rewards. The code and benchmarks are on <a href="http://github.com/tjsingleton/haversine_benchmark">github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://tjsingleton.name/2010/06/haversine-formula-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Danger of the Silent Fail</title>
		<link>http://tjsingleton.name/2010/05/silent-fail/</link>
		<comments>http://tjsingleton.name/2010/05/silent-fail/#comments</comments>
		<pubDate>Wed, 12 May 2010 04:26:09 +0000</pubDate>
		<dc:creator>TJ Singleton</dc:creator>
				<category><![CDATA[Clips]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://tjsingleton.name/?p=244</guid>
		<description><![CDATA[I came across a bug in an rails app I wrote today. This thing was driving me crazy. I was generating a random 10 digit number for my model, however about one-third of the time in production the number was getting set to the same thing, 2147483647. I wrote some specs but couldn't reproduce the [...]]]></description>
			<content:encoded><![CDATA[<p>I came across a bug in an rails app I wrote today. This thing was driving me crazy. I was generating a random 10 digit number for my model, however about one-third of the time in production the number was getting set to the same thing, 2147483647.</p>
<p>I wrote some specs but couldn't reproduce the behavior. I was pretty frustrated. I logged into the server and went into the console to test it out, and it was failing. I'd update the number, save it, reload the instance and there to my dismay was the haunting 2147483647.</p>
<p>What was it? Locally I was using sqlite to run the test, while on the server I was using mysql. 2147483647 is the largest 32bit signed integer. I was overflowing the column, however mysql was silently accepting the larger number, and truncating it to 2147483647.</p>
<p>I would have assumed that trying to set a value greater than available would have raised an exception. Sadly, this wasn't the first time mysql has silently failed on me.  It probably won't be the last. Still goes to demonstrate the frustration that can result from unexpected behavior.</p>
]]></content:encoded>
			<wfw:commentRss>http://tjsingleton.name/2010/05/silent-fail/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Sometimes programming problems are best solved AFK</title>
		<link>http://tjsingleton.name/2009/08/sometimes-programming-problems-are-best-solved-afk/</link>
		<comments>http://tjsingleton.name/2009/08/sometimes-programming-problems-are-best-solved-afk/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 04:07:22 +0000</pubDate>
		<dc:creator>TJ Singleton</dc:creator>
				<category><![CDATA[Clips]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://tjsingleton.name/?p=54</guid>
		<description><![CDATA[This afternoon I ran into a problem. I was working on adding in some new form fields on an edit screen. The fields were for an associated model. I was using fields_for in the view and accepts_nested_attributes_for in the model. Everything looked great, but my specs were still failing. The form just wasn't updating the [...]]]></description>
			<content:encoded><![CDATA[<p>This afternoon I ran into a problem. I was working on adding in some new form fields on an edit screen. The fields were for an associated model. I was using <kbd>fields_for</kbd> in the view and <kbd>accepts_nested_attributes_for</kbd> in the model. Everything looked great, but my specs were still failing. The form just wasn't updating the association.</p>
<p>I desk checked the code. I tried it in <kbd>script/console</kbd>. I checked the controller logs. I tried everything I could think of.  I struggled for an hour or so before I called in for help. By then end of the day, neither a more experienced developer or myself could figure out why it wasn't working.  We were ready to blame plugins, rails, or anything else. Looking over the code, everything looked correct.</p>
<p>Calling the <kbd>@my_model.my_association_attributes= </kbd> worked, but the controller was never calling it! Can you guess what the culprit was?  The model had <kbd>attr_accessible</kbd> set!</p>
<p>So is the moral of the story to add the association_attributes on models with <kbd>attr_accessible</kbd>? Not exactly, see I didn't solve the problem at my desk. I solved the problem on the way home from church about two and a half hours later. I wasn't even thinking about the problem when it popped in my head.</p>
<p>Sometimes the right course of action is to just step away from the problem for awhile, and let our mind work on it. A week or so ago, I had struggled for a couple hours trying to figure out how to solve a particular problem cleanly. I had about half a dozen ways to solve it, but thinking of actually implementing any of them made me feel dirty. I knew there was a good way to solve this problem.</p>
<p>How did I come up with a solution? I slept. When I woke up, brushed my teeth, and fired up textmate the next morning in my head was a elegant (and obvious) solution to the problem at hand. Sometimes our minds just need a break.</p>
<p>Next time you get frustrated, step away for a bit. Let your mind relax and refresh. AFK isn't as bad as it sounds, and sometimes it's medicinal.</p>
]]></content:encoded>
			<wfw:commentRss>http://tjsingleton.name/2009/08/sometimes-programming-problems-are-best-solved-afk/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>You Failed. Autotest with a voice.</title>
		<link>http://tjsingleton.name/2009/08/you-failed-autotest-with-a-voice/</link>
		<comments>http://tjsingleton.name/2009/08/you-failed-autotest-with-a-voice/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 22:59:18 +0000</pubDate>
		<dc:creator>TJ Singleton</dc:creator>
				<category><![CDATA[Clips]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://tjsingleton.name/?p=43</guid>
		<description><![CDATA[While listening to a talk from Ruby Conf 08, I heard Joe Martinez mention the say command in OS X. I figured I'd pop open my .autotest file and wire it up so it'd give me some motivation for when my specs go red. Here is what I added: Autotest.add_hook :red do &#124;autotest&#124; `say -v [...]]]></description>
			<content:encoded><![CDATA[<p>While listening to <a href="http://rubyconf2008.confreaks.com/better-hacking-with-training-wheels.html">a talk from Ruby Conf 08</a>, I heard <a href="http://jrmiii.com/">Joe Martinez</a> mention the <a href="http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/say.1.html"><kbd>say</kbd></a> command in OS X. I figured I'd pop open my .autotest file and wire it up so it'd give me some motivation for when my specs go red. </p>
<p>Here is what I added:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Autotest.<span style="color:#9900CC;">add_hook</span> <span style="color:#ff3333; font-weight:bold;">:red</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>autotest<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#996600;">`say -v &quot;Good News&quot; &quot;You're doing it wrong&quot;`</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://tjsingleton.name/2009/08/you-failed-autotest-with-a-voice/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

