<?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; collect</title>
	<atom:link href="http://www.globalnerdy.com/tag/collect/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>Mon, 22 Mar 2010 01:05:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Enumerating Enumerable: Enumerable#collect/Enumerable#map</title>
		<link>http://www.globalnerdy.com/2008/06/25/enumerating-enumerable-enumerablecollectenumerablemap/</link>
		<comments>http://www.globalnerdy.com/2008/06/25/enumerating-enumerable-enumerablecollectenumerablemap/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 04:00:42 +0000</pubDate>
		<dc:creator>Joey deVilla</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[collect]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[enumerable]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://globalnerdy.com/?p=1768</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;"><a href="http://globalnerdy.com/2008/06/25/enumerating-enumerable-enumerablecollectenumerablemap/"><img src="http://globalnerdy.com/wordpress/wp-content/uploads/2008/06/ruby_enumerablecollect_enumerablemap.jpg" alt="Graphic representation of Ruby\&#039;s \&#34;Enumerable#collect / Enumerable#map\&#34; methods" title="ruby_enumerablecollect_enumerablemap" width="400" height="319" /></a></p>

<p>Here we are the third installment of <cite>Enumerating Enumerable</cite>, my attempt 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 installment, I cover <code>Enumerable#collect</code> and its syntactic sugar twin (and the one I prefer), <code>Enumerable#map</code>.</p>

<p><a href="http://globalnerdy.com/2008/06/25/enumerating-enumerable-enumerablecollectenumerablemap/"><strong>Click here to read about <code>Enumerable#collect</code>/<code>Enumerable#map</code>...</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>Here we are the third installment of <cite>Enumerating Enumerable</cite>, my attempt 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 installment, I cover <code>Enumerable#collect</code> and its syntactic sugar twin (and the one I prefer), <code>Enumerable#map</code>.</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>
</ol>
<h3>Enumerable#collect/Enumerable#map Quick Summary</h3>
<p style="text-align:center;"><img src="http://globalnerdy.com/wordpress/wp-content/uploads/2008/06/ruby_enumerablecollect_enumerablemap.jpg" alt="Graphic representation of Ruby\&#039;s \&quot;Enumerable#collect / Enumerable#map\&quot; methods" title="ruby_enumerablecollect_enumerablemap" width="400" height="319" /></p>
<table>
<tr>
<th>In the simplest possible terms</th>
<td>Create a new array by performing some operation on every item in the given collection. <code>collect</code> and <code>map</code> are synonyms &#8212; you can use either. I personally prefer <code>map</code> as it&#8217;s shorter and makes sense if you think of the method as being like <a href="http://en.wikipedia.org/wiki/Map_(higher-order_function)">functional mapping</a>.</td>
</tr>
<tr>
<th>Ruby version</th>
<td>1.8 and 1.9</td>
</tr>
<tr>
<th>Expects</th>
<td>A block containing the criteria. This block is optional, but you&#8217;re likely to use one in most cases.</td>
</tr>
<tr>
<th>Returns</th>
<td>An array made up of items created by performing some operation on the given collection.</td>
</tr>
<tr>
<th>RubyDoc.org&#8217;s entry</th>
<td><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M001145">Enumerable#collect</a> / <a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M001146">Enumerable#map</a></td>
</tr>
</table>
<h3>Enumerable#collect/Enumerable#map and Arrays</h3>
<p>When used on an array and a block is provided, <code>collect</code>/<code>map</code> passes each item to the block, where the operation in the block is performed on the item and the result is then added to the result array. Note the the result array has the same number of elements as the given array.</p>
<p><code>
<pre>
[1, 2, 3, 4].map {|number| number ** 2}
=> [1, 4, 9, 16]

["Aqua", "Bat", "Super", "Wonder Wo"].map {|adjective| adjective + "man"}
=> ["Aquaman", "Batman", "Superman", "Wonder Woman"]
</pre>
<p></code></p>
<p>When the block is omitted, <code>collect</code>/<code>map</code> uses this implied block: <code>{|item| item}</code>, which means when applied on an array without a block, <code>collect</code>/<code>map</code> is the <em>identity function</em> &#8212; the resulting array is the same as the given array.</p>
<p><code>
<pre>
[1, 2, 3, 4].map
=> [1, 2, 3, 4]

["Aqua", "Bat", "Super", "Wonder Wo"].map
=> ["Aqua", "Bat", "Super", "Wonder Wo"]
</pre>
<p></code></p>
<h3>Enumerable#collect/Enumerable#map and Hashes</h3>
<p>When used on a hash and a block is provided, <code>collect</code> and <code>map</code> pass 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, where the operation in the block is performed on the item and the result is then added to the result array. Note the the result array has the same number of elements as the given array.</p>
<p><code>
<pre>
burgers = {"Big Mac" => 300, "Whopper with cheese" => 450, "Wendy's Double with cheese" => 320}

# What if I had just half a burger?
burgers.map {|burger| burger[1] / 2}
=> [160, 150, 225]

burgers.map {|sandwich, calories| calories / 2}
=> [160, 150, 225]

burgers.map {|burger| "Have a tasty #{burger[0]}!"}
=> ["Have a tasty Wendy's Double with cheese!", "Have a tasty Big Mac!",
 "Have a tasty Whopper with cheese!"]

burgers.map {|sandwich, calories| "Have a tasty #{sandwich}!"}
=> ["Have a tasty Wendy's Double with cheese!", "Have a tasty Big Mac!",
 "Have a tasty Whopper with cheese!"]

burgers.map {|sandwich, calories| ["Half a #{sandwich}", calories / 2]}
=> [["Half a Wendy's Double with cheese", 160], ["Half a Big Mac", 150],
 ["Half a Whopper with cheese", 225]]
</pre>
<p></code></p>
<p>When the block is omitted, <code>collect</code>/<code>map</code> uses this implied block: <code>{|item| item}</code>, which means when applied on an hash without a block, <code>collect</code>/<code>map</code> returns an array containing a set of two-item arrays, one for each key/value pair in the hash. For each two-item array, item 0 is the key and item 1 is the corresponding value.</p>
<p><code>
<pre>
burgers = {"Big Mac" => 300, "Whopper with cheese" => 450, "Wendy's Double with cheese" => 320}

burgers.map
=> [["Wendy's Double with cheese", 320], ["Big Mac", 300], ["Whopper with cheese", 450]]
</pre>
<p></code></p>
<h3>Special Case: Using Enumerable#collect/Enumerable#map on Empty Arrays and Hashes</h3>
<p>When applied to an empty array or hash, with or without a block, <code>collect</code> and <code>map</code> always return an empty array.</p>
<p><code>
<pre>
# Let's try it with an empty array
[].map
=> []

[].map {|item| item * 2}
=> []

# Now let's try it with an empty hash
{}.map
=> []

{}.map {|sandwich, calories| "Have a tasty #{sandwich}!"}
=> []
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.globalnerdy.com/2008/06/25/enumerating-enumerable-enumerablecollectenumerablemap/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
