<?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; collections</title>
	<atom:link href="http://www.globalnerdy.com/tag/collections/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, 11 Mar 2010 16:59:48 +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>
		<item>
		<title>Enumerating Enumerable: Enumerable#any?</title>
		<link>http://www.globalnerdy.com/2008/06/24/enumerating-enumerable-enumerableany/</link>
		<comments>http://www.globalnerdy.com/2008/06/24/enumerating-enumerable-enumerableany/#comments</comments>
		<pubDate>Tue, 24 Jun 2008 04:00:39 +0000</pubDate>
		<dc:creator>Joey deVilla</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[any?]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[enumerable]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://globalnerdy.com/?p=1767</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/24/enumerating-enumerable-enumerableany/"><img src="http://globalnerdy.com/wordpress/wp-content/uploads/2008/06/ruby_enumerableany.jpg" alt="Graphic representing Ruby\&#039;s Enumerable#any? method" title="ruby_enumerableany" width="201" height="310" /></a></p>

<p>Welcome to the second installment of <cite>Enumerating Enumerable</cite>, my project to do a better job of documenting Ruby's <code>Enumerable</code> module than <a href="http://rubydoc.org/">RubyDoc.org</a>. This installment will cover <code>Enumerable#any?</code>, which I like to think of as <code>Enumerable.all?</code>'s more easy-going cousin (I covered <code>Enumerable.all?</code> in <a href="http://globalnerdy.com/2008/06/23/enumerating-enumerable-enumerableall/">the previous installment</a>).</p>

<p><a href="http://globalnerdy.com/2008/06/24/enumerating-enumerable-enumerableany/"><strong>Click here to read about <code>Enumerable#any?</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>Welcome to the second installment of <cite>Enumerating Enumerable</cite>, my project to do a better job of documenting Ruby&#8217;s <code>Enumerable</code> module than <a href="http://rubydoc.org/">RubyDoc.org</a>. This installment will cover <code>Enumerable#any?</code>, which I like to think of as <code>Enumerable.all?</code>&#8217;s more easy-going cousin (I covered <code>Enumerable.all?</code> in <a href="http://globalnerdy.com/2008/06/23/enumerating-enumerable-enumerableall/">the previous installment</a>).</p>
<h3>Enumerable#any? Quick Summary</h3>
<p style="text-align:center;"><img src="http://globalnerdy.com/wordpress/wp-content/uploads/2008/06/ruby_enumerableany.jpg" alt="Graphic representing Ruby\&#039;s Enumerable#any? method" title="ruby_enumerableany" width="201" height="310" /></p>
<table>
<tr>
<th>In the simplest possible terms</th>
<td>Do any of the items in the collection meet the given criteria?</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><code>true</code> if any of the items in the collection meet the given criteria.</p>
<p>        <code>false</code> if none of the items in the collection does not 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#M001153">Enumerable#any?</a></td>
</tr>
</table>
<h3>Enumerable#any? and Arrays</h3>
<p>When used on an array and a block is provided, <code>any?</code> passes each item to the block. If the block returns <code>true</code> for any item during this process, <code>any?</code> returns <code>true</code>; otherwise, it returns <code>false</code>.</p>
<p><code>
<pre>
# "Fromage de Montagne de Savoie" is the longest-named cheese in this list
# at a whopping 29 characters
cheeses = ["feta", "cheddar", "stilton", "camembert", "mozzarella", "Fromage de Montagne de Savoie"]

cheeses.any? {|cheese| cheese.length >= 25}
=> true

cheeses.any? {|cheese| cheese.length >= 35}
=> false
</pre>
<p></code></p>
<p>When the block is omitted, <code>any?</code> uses this implied block: <code>{|item| item}</code>. Since everything in Ruby evaluates to <code>true</code> except for <code>false</code> and <code>nil</code>, using <code>any?</code> without a block is effectively a test to see if any of the items in the collection evaluate to <code>true</code> (or conversely, if all the values in the array evaluate to  <code>false</code> or <code>nil</code>).</p>
<p><code>
<pre>
cheeses.any?
=> true

cheeses = [false, nil]
=> [false, nil]

cheeses.any?
=> false

# Remember that in Ruby, everything except for false and nil evaluates to true:
cheeses << 0
=> [false, nil, 0]

>> cheeses.any?
=> true
</pre>
<p></code></p>
<h3>Enumerable#any? and Hashes</h3>
<p>When used on a hash and a block is provided, <code>any?</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>If the block returns <code>true</code> for any item during this process, <code>any?</code> returns <code>true</code>; otherwise, it returns <code>false</code>.</p>
<p><code>
<pre>
# Here's a hash where for each key/value pair, the key is a programming language and
# the corresponding value is the year when that language was first released
# The keys range in value from "Javascript" to "Ruby", and the values range from
# 1987 to 1996
languages = {"Javascript" => 1996, "PHP" => 1994, "Perl" => 1987, "Python" => 1991, "Ruby" => 1993}

languages.any? {|language| language[0] < "Pascal"}
=> true

languages.any? {|language, year_created| language < "Pascal"}
=> true

languages.any? {|language| language[0] < "Fortran"}
=> false

languages.any? {|language, year_created| language < "Fortran"}
=> false

languages.any? {|language| language[0] >= "Basic" and language[1] <= 1995}
=> true

languages.any? {|language, year_created| language >= "Basic" and year_created <= 1995}
=> true

languages.any? {|language| language[0] >= "Basic" and language[1] <= 1985}
=> false

languages.any? {|language, year_created| language >= "Basic" and year_created <= 1985}
=> false
</pre>
<p></code></p>
<p>Using <code>any?</code> without a block on a hash is meaningless, as it will always return <code>true</code>. When the block is omitted, <code>any?</code> uses this implied block: <code>{|item| item}</code>. In the case of a hash, <code>item</code> will always be a two-element array, which means that it will never evaluate as <code>false</code> nor <code>nil</code>.</p>
<p>And yes, even this hash, when run through <code>any?</code>, will still return <code>true</code>:</p>
<p><code>
<pre>
{false => false, nil => nil}.any?
=> true
</pre>
<p></code></p>
<h3>Special Case: Using Enumerable#any? on Empty Arrays and Hashes</h3>
<p>When applied to an empty array or hash, with or without a block, <code>any?</code> always returns <code>false</code>. That’s because with an empty collection, there are no values to process and return a <code>true</code> value.</p>
<p>Let’s look at the case of empty arrays:</p>
<p><code>
<pre>
cheeses = []
=> []

cheeses.any? {|cheese| cheese.length >= 25}
=> false

cheeses.any?
=> false

# Let's try applying "any?" to a non-empty array
# using a block that ALWAYS returns true:
["Gruyere"].any? {|cheese| true}
=> true

# ...but watch what happens when we try the same thing
# with an EMPTY array!
[].any? {|cheese| true}
=> false
</pre>
<p></code></p>
<p>…now let’s look at the case of empty hashes:</p>
<p><code>
<pre>
languages = {}
=> {}

languages.any? {|language| language[0] < "Pascal"}
=> false

languages.any? {|language, year_created| language < "Pascal"}
=> false

languages.any?
=> false

# Let's try applying "any?" to a non-empty hash
# using a block that ALWAYS returns true:
{"Lisp" => 1959}.any? {|language| true}
=> true

# ...but watch what happens when we try the same thing
# with an EMPTY hash!
{}.any? {|language| true}
=> false
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.globalnerdy.com/2008/06/24/enumerating-enumerable-enumerableany/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
