<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>MettaProgramming &#187; duck typing</title>
	<atom:link href="http://mettadore.com/tag/duck-typing/feed/" rel="self" type="application/rss+xml" />
	<link>http://mettadore.com</link>
	<description>Thoughts on Software and Technology</description>
	<lastBuildDate>Mon, 09 Apr 2012 19:11:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>If it quacks like a duck &amp; walks like a duck…</title>
		<link>http://mettadore.com/ruby/if-it-quacks-like-a-duck-walks-like-a-duck%e2%80%a6/</link>
		<comments>http://mettadore.com/ruby/if-it-quacks-like-a-duck-walks-like-a-duck%e2%80%a6/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 16:30:15 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[duck typing]]></category>

		<guid isPermaLink="false">http://mettadore.com/?p=99</guid>
		<description><![CDATA[…it may actually be a penquin in disguise! Ahh, the throws of young love. Remember that first wild romance? The incredible highs, the tragic lows, the fact that the swung from one to the other in 30 second intervals? I had an interesting afternoon yesterday fighting with my new lover, Ruby. I&#8217;m building a data [...]]]></description>
			<content:encoded><![CDATA[<h2>…it may actually be a penquin in disguise!</h2>
<p>Ahh, the throws of young love. Remember that first wild romance? The incredible highs, the tragic lows, the fact that the swung from one to the other in 30 second intervals?</p>
<p>I had an interesting afternoon yesterday fighting with my new lover, Ruby.</p>
<p>I&#8217;m building a data processing application and started playing around with FasterCSV. It&#8217;s a great library, with the possible exception that it may secretly be giving me evil penguin pseudo-duck classes.<span id="more-99"></span></p>
<p>From various examples:</p>
<pre class="brush: ruby; title: ; notranslate">
FasterCSV.foreach('/path/to/file') do |row|
  foo = row[0]
  bar = row[1]
  puts foo + bar
end
</pre>
<p>Pretty nice. FasterCSV returns an array of values. It&#8217;s a fast way to access CSV files, especially using it&#8217;s :header, :col_sep and other arguments which would allow nice programmatic handing of CSV files easily.</p>
<p>But I had a problem. I&#8217;m used to fast one-liners and don&#8217;t like to spend all day setting variables when I really want to be calculating dissolved oxygen percentage. Thus I went straight to:</p>
<pre class="brush: ruby; title: ; notranslate">
FasterCSV.foreach('/cool/scientific/data') do |row|
  temp, cond, ph, domgl, depth, sal, turb = row[2..8]
  …
  blah = temp * 375
  …
end
</pre>
<p><a href="http://en.wikipedia.org/wiki/The_Wrong_Trousers"><img class="size-full wp-image-100 alignleft" src="http://mettadore.com/files/2010/01/feathers.gif" alt="Evil penquin pseudo-duck classes!" width="94" height="148" /></a></p>
<h3>Crash!</h3>
<p>Well, not really crash. We love that word, though, don&#8217;t we. &#8220;My Excel crashed!&#8221; I&#8217;ll restate for clarity:</p>
<p><strong>Exception!</strong></p>
<p>Apparently, multiplying nil by an integer is an exception.</p>
<p>&#8220;What? Do I have blank line? How is temp nil?&#8221;</p>
<p>Well, you real Rubologists may know, but I didn&#8217;t. I thought that I could shortcut those variables, but it didn&#8217;t work! It took a long time, and I read more about Ruby slicing than I wanted to.</p>
<p>Turns out that you can, of course, shortcut those variables… provided that method exists. I guess it doesn&#8217;t in the duck-typed class returned by FasterCSV. Hunting around, I found that our friend row had a &#8216;to_a&#8217; method. Thus, this works.</p>
<pre class="brush: ruby; title: ; notranslate">
&lt;pre&gt;FasterCSV.foreach('/cool/scientific/data') do |row|
  temp, cond, ph, domgl, depth, sal, turb = row.to_a![2..8]
  …
  blah = temp * 375&lt;/pre&gt;
  …
end
</pre>
<p>So, the lesson here: Just because examples tell you it&#8217;s an array, and because you can pull a value like an array, don&#8217;t assume that it <em>is</em> an array.</p>
<p>Apparently, this thing didn&#8217;t act quite enough like a duck.</p>
]]></content:encoded>
			<wfw:commentRss>http://mettadore.com/ruby/if-it-quacks-like-a-duck-walks-like-a-duck%e2%80%a6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

