<?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; any?</title>
	<atom:link href="http://www.globalnerdy.com/tag/any/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#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>
