<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Floehopper: Why Array#length is better than Array#size</title>
    <link>http://www.floehopper.org/articles/2007/08/07/why-array-length-is-better-than-array-size</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts on the bergy bits of life</description>
    <item>
      <title>Why Array#length is better than Array#size</title>
      <description>&lt;p&gt;Recently &lt;a href="http://www.lukeredpath.co.uk/"&gt;Luke&lt;/a&gt; and I spotted a subtle bug in our code where we were mistakenly assuming a method returned an &lt;code&gt;Array&lt;/code&gt; when it actually returned a &lt;code&gt;Fixnum&lt;/code&gt;. We were then calling the &lt;code&gt;#size&lt;/code&gt; method on the result, but interestingly &lt;a href="http://www.ruby-doc.org/core/classes/Fixnum.html#M001098"&gt;&lt;code&gt;Fixnum#size&lt;/code&gt;&lt;/a&gt; returns the number of bytes in its machine representation.&lt;/p&gt;


	&lt;p&gt;Previously I had noticed that my colleague &lt;a href="http://po-ru.com/"&gt;Paul Battley&lt;/a&gt; tended to use &lt;a href="http://www.ruby-doc.org/core/classes/Array.html#M002199"&gt;&lt;code&gt;Array#length&lt;/code&gt;&lt;/a&gt; in preference to &lt;a href="http://www.ruby-doc.org/core/classes/Array.html#M002244"&gt;&lt;code&gt;Array#size&lt;/code&gt;&lt;/a&gt;. Now I know why, or at least I know why I&amp;#8217;m going to start using &lt;code&gt;Array#length&lt;/code&gt;! If we&amp;#8217;d used &lt;code&gt;#length&lt;/code&gt; in this case we would have seen a &lt;code&gt;NoMethodError&lt;/code&gt; instead of the spurious value, 4.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;  &lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;5&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;&lt;span class="number"&gt;6&lt;/span&gt;&lt;span class="punct"&gt;].&lt;/span&gt;&lt;span class="ident"&gt;size&lt;/span&gt; &lt;span class="comment"&gt;# =&amp;gt; 2&lt;/span&gt;
  &lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;size&lt;/span&gt; &lt;span class="comment"&gt;# =&amp;gt; 4&lt;/span&gt;
  &lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;5&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;&lt;span class="number"&gt;6&lt;/span&gt;&lt;span class="punct"&gt;].&lt;/span&gt;&lt;span class="ident"&gt;length&lt;/span&gt; &lt;span class="comment"&gt;# =&amp;gt; 2&lt;/span&gt;
  &lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;length&lt;/span&gt; &lt;span class="comment"&gt;# =&amp;gt; NoMethodError: undefined method `length' for 1:Fixnum&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Josh Susser has posted a &lt;a href="http://blog.hasmanythrough.com/2008/2/27/count-length-size"&gt;useful related article&lt;/a&gt; about the use of&amp;#8230;&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://dev.rubyonrails.org/browser/tags/rel_2-0-2/activerecord/lib/active_record/associations/has_many_association.rb?rev=8940#L18"&gt;ActiveRecord::Associations::HasManyAssociation#count&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://dev.rubyonrails.org/browser/tags/rel_2-0-2/activerecord/lib/active_record/associations/association_collection.rb?rev=8940#L103"&gt;ActiveRecord::Associations::AssocationCollection#size&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://dev.rubyonrails.org/browser/tags/rel_2-0-2/activerecord/lib/active_record/associations/association_collection.rb?rev=8940#L116"&gt;ActiveRecord::Associations::AssocationCollection#length&lt;/a&gt;.&lt;/li&gt;
	&lt;/ul&gt;</description>
      <pubDate>Tue, 07 Aug 2007 17:05:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:323bceaf-7ac2-4900-b200-2b201dbb2ff2</guid>
      <author>James Mead</author>
      <link>http://www.floehopper.org/articles/2007/08/07/why-array-length-is-better-than-array-size</link>
      <category>array</category>
      <category>ruby</category>
      <category>size</category>
      <category>length</category>
      <category>machine</category>
      <category>representation</category>
      <category>bug</category>
    </item>
    <item>
      <title>"Why Array#length is better than Array#size" by James Mead</title>
      <description>&lt;p&gt;Dave: I don&amp;#8217;t want to explicitly check that the object is an array, because I&amp;#8217;m assuming it is an array. But on the offchance it isn&amp;#8217;t I&amp;#8217;d prefer it to fail fast and not give me a dodgy value.&lt;/p&gt;</description>
      <pubDate>Thu, 13 Sep 2007 21:44:40 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:6aa1f879-a836-486c-b93a-01e98b216e2f</guid>
      <link>http://www.floehopper.org/articles/2007/08/07/why-array-length-is-better-than-array-size#comment-289</link>
    </item>
    <item>
      <title>"Why Array#length is better than Array#size" by Benjamin Kudria</title>
      <description>&lt;p&gt;Hmm, is this an argument for strict type checking, or an problem arising from English semantics?&lt;/p&gt;</description>
      <pubDate>Fri, 31 Aug 2007 21:57:04 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0a9087a2-8805-4ed4-bc16-50861e7244f6</guid>
      <link>http://www.floehopper.org/articles/2007/08/07/why-array-length-is-better-than-array-size#comment-288</link>
    </item>
    <item>
      <title>"Why Array#length is better than Array#size" by Dave</title>
      <description>&lt;p&gt;...or rather why do you prefer &lt;code&gt;length&lt;/code&gt;?&lt;/p&gt;</description>
      <pubDate>Wed, 08 Aug 2007 09:07:36 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e56f2beb-c204-4150-aff0-fa18faccd955</guid>
      <link>http://www.floehopper.org/articles/2007/08/07/why-array-length-is-better-than-array-size#comment-285</link>
    </item>
    <item>
      <title>"Why Array#length is better than Array#size" by Dave</title>
      <description>&lt;p&gt;Hello,
What&amp;#8217;s wrong with
&lt;br /&gt;
&lt;code&gt;
[5,6]is_a? Array #=&amp;gt; true
&lt;/code&gt;
&lt;br /&gt;
&lt;code&gt;
6.is_a? Array #=&amp;gt; false
&lt;/code&gt;
&lt;br /&gt;
?
Dave&lt;/p&gt;</description>
      <pubDate>Wed, 08 Aug 2007 09:00:13 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a3f9bbe0-0266-42ce-8773-4ecd67a92fda</guid>
      <link>http://www.floehopper.org/articles/2007/08/07/why-array-length-is-better-than-array-size#comment-284</link>
    </item>
  </channel>
</rss>
