<?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>Global Nerdy &#187; count</title>
	<atom:link href="http://www.globalnerdy.com/tag/count/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.globalnerdy.com</link>
	<description>Tech Evangelist Joey deVilla on software development, tech news and other nerdy stuff</description>
	<lastBuildDate>Thu, 19 Nov 2009 02:16:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Enumerating Enumerable: Enumerable#count</title>
		<link>http://www.globalnerdy.com/2008/07/02/enumerating-enumerable-enumerablecount/</link>
		<comments>http://www.globalnerdy.com/2008/07/02/enumerating-enumerable-enumerablecount/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 18:19:27 +0000</pubDate>
		<dc:creator>Joey deVilla</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[enumerable]]></category>
		<category><![CDATA[Enumerating Enumerable]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby 1.9]]></category>

		<guid isPermaLink="false">http://globalnerdy.com/?p=1771</guid>
		<description><![CDATA[<p style="text-align:center;"><img src="http://globalnerdy.com/wordpress/wp-content/uploads/2008/06/enumerating_enumerable.jpg" alt="" title="enumerating_enumerable" width="350" height="120" /></p>

<p style="text-align:center;"><img src="http://globalnerdy.com/wordpress/wp-content/uploads/2008/07/ruby_enumerablecount.jpg" alt="Graphic representation of the Enumberable#count method in Ruby" title="Graphic representation of the Enumerable#count method in Ruby" width="201" height="310" /></p>

<p>Welcome to the fourth installment of <cite>Enumerating Enumerable</cite>, a series of articles in which I challenge myself to do a better job of documenting Ruby's <code>Enumerable</code> module than <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">RubyDoc.org does</a>. In this article, I'll cover <code>Enumerable#count</code>, one of the new methods added to Enumerable in Ruby 1.9.</p>

<p>In case you missed the earlier installments, they're listed (and linked) below:
<ol>
<li><a href="http://globalnerdy.com/2008/06/23/enumerating-enumerable-enumerableall/">all?</a></li>
<li><a href="http://globalnerdy.com/2008/06/24/enumerating-enumerable-enumerableany/">any?</a></li>
<li><a href="http://globalnerdy.com/2008/06/25/enumerating-enumerable-enumerablecollectenumerablemap/">collect / map</a></li>
</ol>
</p>

<p><a href="http://globalnerdy.com/2008/07/02/enumerating-enumerable-enumerablecount/"><strong>Read on for more about Enumerable#count...</strong></a></p>]]></description>
			<content:encoded><![CDATA[<p></p><p style="text-align:center;"><img src="http://globalnerdy.com/wordpress/wp-content/uploads/2008/06/enumerating_enumerable.jpg" alt="" title="enumerating_enumerable" width="350" height="120" /></p>
<p>Welcome to the fourth installment of <cite>Enumerating Enumerable</cite>, a series of articles in which I challenge myself to do a better job of documenting Ruby&#8217;s <code>Enumerable</code> module than <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">RubyDoc.org does</a>. In this article, I&#8217;ll cover <code>Enumerable#count</code>, one of the new methods added to Enumerable in Ruby 1.9.</p>
<p>In case you missed the earlier installments, they&#8217;re listed (and linked) below:</p>
<ol>
<li><a href="http://globalnerdy.com/2008/06/23/enumerating-enumerable-enumerableall/">all?</a></li>
<li><a href="http://globalnerdy.com/2008/06/24/enumerating-enumerable-enumerableany/">any?</a></li>
<li><a href="http://globalnerdy.com/2008/06/25/enumerating-enumerable-enumerablecollectenumerablemap/">collect / map</a></li>
</ol>
<h3>Enumerable#count Quick Summary</h3>
<p style="text-align:center;"><img src="http://globalnerdy.com/wordpress/wp-content/uploads/2008/07/ruby_enumerablecount.jpg" alt="Graphic representation of the Enumberable#count method in Ruby" title="Graphic representation of the Enumerable#count method in Ruby" width="201" height="310" /></p>
<table>
<tr>
<th>In the simplest possible terms</th>
<td>How many items in the collection meet the given criteria?</td>
</tr>
<tr>
<th>Ruby version</th>
<td>1.9 only</td>
</tr>
<tr>
<th>Expects</th>
<td>Either:</p>
<ul>
<li>An argument to be matched against the items in the collection</li>
<li>A block containing an expression to test the items in the collection</li>
</ul>
</td>
</tr>
<tr>
<th>Returns</th>
<td>The number of items in the collection that meet the given criteria.</td>
</tr>
<tr>
<th>RubyDoc.org&#8217;s entry</th>
<td><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M001138">Enumerable#count</a></td>
</tr>
</table>
<h3>Enumerable#count and Arrays</h3>
<p>When used on an array and an argument is provided, <code>count</code> returns the number of times the value of the argument appears in the array:</p>
<p><code>
<pre>
# How many instances of "zoom" are there in the array?
["zoom", "schwartz", "profigliano", "zoom"].count("zoom")
=> 2

# Prior to Ruby 1.9, you'd have to use this equivalent code:
["zoom", "schwartz", "profigliano", "zoom"].select {|word| word == "zoom"}.size
=> 2
</pre>
<p></code></p>
<p>When used on an array and a block is provided, <code>count</code> returns the number of items in the array for which the block returns <code>true</code>:</p>
<p><code>
<pre>
# How many members of "The Mosquitoes" (a Beatles-esque band that appeared on
# "Gilligan's Island") have names following the "B*ngo" format?
["Bingo", "Bango", "Bongo", "Irving"].count {|bandmate| bandmate =~ /B[a-z]ngo/}
=> 3

# Prior to Ruby 1.9, you'd have to use this equivalent code:
["Bingo", "Bango", "Bongo", "Irving"].select {|bandmate| bandmate =~ /B[a-z]ngo/}.size
</pre>
<p></code></p>
<p>RubyDoc.org says that when <code>count</code> is used on an array without an argument or a block, it simply returns the number of items in the array (which is what the <code>length</code>/<code>size</code> methods do). However, when I&#8217;ve tried it in <strong>irb</strong> and <strong>ruby</strong>, I got results like this:</p>
<p><code>
<pre>
[1, 2, 3, 4].count
=> #&lt;Enumerable::Enumerator:0x189d784&gt;
</pre>
<p></code></p>
<h3>Enumerable#count and Hashes</h3>
<p>As with arrays, when used on a hash and an argument is provided, <code>count</code> returns the number of times the value of the argument appears in the hash. The difference is that for the comparison, each key/value pair is treated as a two-element array, with the key being element 0 and the value being element 1.</p>
<p><code>
<pre>
# Here's a hash where the names of recent movies are keys
# and their metacritic.com ratings are the corresponding values.
movie_ratings = {"Get Smart" => 53, "Kung Fu Panda" => 88, "The Love Guru" => 15,
"Sex and the City" => 51, "Iron Man" => 93}
=> {"Get Smart"=>53, "Kung Fu Panda"=>88, "The Love Guru"=>15, "Sex and the City"=>51, "Iron Man"=>93}

# This statement will return a count of 0, since there is no item in movie_ratings
# that's just plain "Iron Man".
movie_ratings.count("Iron Man")
=> 0

# This statement will return a count of 1, since there is an item in movie_ratings
# with the key "Iron Man" and the corresponding value 93.
movie_ratings.count(["Iron Man", 93])
=> 1

# This statement will return a count of 0. There's an item in movie_ratings
# with the key "Iron Man", but its corresponding value is NOT 92.
movie_ratings.count(["Iron Man", 92])
=> 0
</pre>
<p></code></p>
<p><code>count</code> is not useful when used with a hash and an argument. It will only ever return two values:</p>
<ul>
<li><code>1</code> if the argument is a two-element array and there is an item in the hash whose key matches element [0] of the array and whose value matches element [1] of the array.</li>
<li><code>0</code> for all other cases.</li>
</ul>
<p>When used with a hash and a block, <code>count</code> is more useful. <code>count</code> passes each key/value pair in the hash to the block, which you can &#8220;catch&#8221; as either:</p>
<ol>
<li>A two-element array, with the key as element 0 and its corresponding value as element 1, or</li>
<li>Two separate items, with the key as the first item and its corresponding value as the second item.</li>
</ol>
<p>Each key/value pair is passed to the block and <code>count</code> returns the number of items in the hash for which the block returns <code>true</code>.</p>
<p><code>
<pre>
# Once again, a hash where the names of recent movies are keys
# and their metacritic.com ratings are the corresponding values.
movie_ratings = {"Get Smart" => 53, "Kung Fu Panda" => 88, "The Love Guru" => 15,
 "Sex and the City" => 51, "Iron Man" => 93}
=> {"Get Smart"=>53, "Kung Fu Panda"=>88, "The Love Guru"=>15, "Sex and the City"=>51, "Iron Man"=>93}

# How many movie titles in the collection start
# in the first half of the alphabet?
# (Using a one-parameter block)
movie_ratings.count {|movie| movie[0] <= "M"}
=> 3

# How many movie titles in the collection start
# in the first half of the alphabet?
# (This time using a two-parameter block)
movie_ratings.count {|title, rating| title <= "M"}
=> 3

# Here's how you'd do it in pre-1.9 Ruby:
movie_ratings.select {|movie| movie[0] <= "M"}.size
=> 3
# or...
movie_ratings.select {|title, rating| title <= "M"}.size
=> 3

# How many movies in the collection had a rating
# higher than 80?
# (Using a one-parameter block)
movie_ratings.count {|movie| movie[1] > 80}
=> 2

# How many movies in the collection had a rating
# higher than 80?
# (This time using a two-parameter block)
movie_ratings.count {|title, rating| rating > 80}
=> 2

# Here's how you'd do it in pre-1.9 Ruby:
movie_ratings.select {|title, rating| rating > 80}.size
=> 2

# How many movies in the collection have both:
# - A title starting in the second half of the alphabet?
# - A rating less than 50?
# (Using a one-parameter block)
movie_ratings.count {|movie| movie[0] >= "M" &#038;&#038; movie[1] < 50}
=> 1

# How many movies in the collection have both:
# - A title starting in the second half of the alphabet?
# - A rating less than 50?
# (This time using a two-parameter block)
movie_ratings.count {|title, rating| title >= "M" &#038;&#038; rating < 50}
=> 1

# Here's how you'd do it in pre-1.9 Ruby:
movie_ratings.select {|title, rating| title >= "M" &#038;&#038; rating < 50}.size
=> 1
</pre>
<p></code></p>
<p>(You should probably skip <cite>The Love Guru</cite> completely, or at least until it gets aired on TV for free.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.globalnerdy.com/2008/07/02/enumerating-enumerable-enumerablecount/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
