<?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; drop_while</title>
	<atom:link href="http://www.globalnerdy.com/tag/drop_while/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>Sun, 14 Mar 2010 17:13:30 +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#drop_while</title>
		<link>http://www.globalnerdy.com/2008/07/25/enumerating-enumerable-enumerabledrop_while/</link>
		<comments>http://www.globalnerdy.com/2008/07/25/enumerating-enumerable-enumerabledrop_while/#comments</comments>
		<pubDate>Sat, 26 Jul 2008 02:28:26 +0000</pubDate>
		<dc:creator>Joey deVilla</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[drop_while]]></category>
		<category><![CDATA[enumerable]]></category>
		<category><![CDATA[Enumerating Enumerable]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://globalnerdy.com/?p=1773</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>After the wackiness of the past couple of weeks -- some travel to see family, followed by <a href="http://globalnerdy.com/2008/07/14/this-week-in-toronto-tech/">a busy week of tech events including DemoCamp 18, Damian Conway's presentation, FAILCamp and RubyFringe</a> -- I'm happy to return to <cite>Enumerating Enumerable</cite>, the article series in which I attempt to do a better job at documenting Ruby's <code>Enumerable</code> module than Ruby-Doc.org does.</p>

<p style="text-align:center;"><img src="http://globalnerdy.com/wordpress/wp-content/uploads/2008/07/ruby_enumerabledrop_while.jpg" alt="Graphic representation of the \&#34;drop_while\&#34; method in Ruby\&#039;s \&#34;Enumerable\&#34; module" title="Graphic representation of the \&#34;drop_while\&#34; method in Ruby\&#039;s \&#34;Enumerable\&#34; module" width="257" height="251" class="size-full wp-image-1890" /></p>

<p>In this article, the eighth in the series, I'm going to cover a method introduced in Ruby 1.9: <code>drop_while</code>.</p>

<p>I'm going through the <code>Enumerable</code>'s methods in alphabetical order. If you missed any of the earlier articles, I've listed them all 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>
    <li><a href="http://globalnerdy.com/2008/07/02/enumerating-enumerable-enumerablecount/">count</a></li>
    <li><a href="http://globalnerdy.com/2008/07/06/enumerating-enumerable-enumerablecycle/">cycle</a></li>
    <li><a href="http://globalnerdy.com/2008/07/07/enumerating-enumerable-enumerabledetectenumerablefind/">detect / find</a></li>
    <li><a href="http://globalnerdy.com/2008/07/10/enumerating-enumerable-enumerabledrop/">drop</a></li>
</ol>]]></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>After the wackiness of the past couple of weeks &#8212; some travel to see family, followed by <a href="http://globalnerdy.com/2008/07/14/this-week-in-toronto-tech/">a busy week of tech events including DemoCamp 18, Damian Conway&#8217;s presentation, FAILCamp and RubyFringe</a> &#8212; I&#8217;m happy to return to <cite>Enumerating Enumerable</cite>, the article series in which I attempt to do a better job at documenting Ruby&#8217;s <code>Enumerable</code> module than Ruby-Doc.org does.</p>
<p>In this article, the eighth in the series, I&#8217;m going to cover a method introduced in Ruby 1.9: <code>drop_while</code>.</p>
<p>I&#8217;m going through the <code>Enumerable</code>&#8217;s methods in alphabetical order. If you missed any of the earlier articles, I&#8217;ve listed them all 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>
<li><a href="http://globalnerdy.com/2008/07/02/enumerating-enumerable-enumerablecount/">count</a></li>
<li><a href="http://globalnerdy.com/2008/07/06/enumerating-enumerable-enumerablecycle/">cycle</a></li>
<li><a href="http://globalnerdy.com/2008/07/07/enumerating-enumerable-enumerabledetectenumerablefind/">detect / find</a></li>
<li><a href="http://globalnerdy.com/2008/07/10/enumerating-enumerable-enumerabledrop/">drop</a></li>
</ol>
<h3>Enumerable#drop_while Quick Summary</h3>
<p style="text-align:center;"><img src="http://globalnerdy.com/wordpress/wp-content/uploads/2008/07/ruby_enumerabledrop_while.jpg" alt="Graphic representation of the \&quot;drop_while\&quot; method in Ruby\&#039;s \&quot;Enumerable\&quot; module" title="Graphic representation of the \&quot;drop_while\&quot; method in Ruby\&#039;s \&quot;Enumerable\&quot; module" width="257" height="251" class="size-full wp-image-1890" /></p>
<table>
<tr>
<th>In the simplest possible terms</th>
<td>Given a collection and a condition, return an array made of the collection&#8217;s items, starting the first item that <em>doesn&#8217;t</em> meet the condition.</td>
</tr>
<tr>
<th>Ruby version</th>
<td>1.9 only</td>
</tr>
<tr>
<th>Expects</th>
<td>A block containing the condition.</td>
</tr>
<tr>
<th>Returns</th>
<td>An array made up of the remaining items, if there are any.</td>
</tr>
<tr>
<th>RubyDoc.org&#8217;s entry</th>
<td><a href="http://ruby-doc.org/core/classes/Enumerable.html#M001171">Enumerable#drop_while</a></td>
</tr>
</table>
<h3>Enumerable#drop_while and Arrays</h3>
<p>When used on an array, <code>drop_while</code> returns a copy of the array created by going through the original array&#8217;s items in order, dropping elements until it encounters the an element that <em>does not</em> meet the condition. The resulting array is basically a copy of the original array, starting at the first element that doesn&#8217;t meet the condition in the block.</p>
<p>As in many cases, things become clearer with some examples:</p>
<p><code>
<pre>
# In Canada, and in fact in all but 2 countries in the world,
# the weather report gives temperatures in Celsius!
temperatures = [28, 25, 30, 22, 27]
=> [28, 25, 30, 22, 27]

# The block returns true for the FIRST two elements,
# and false for the third.
# So drop_while returns an array like the original,
# but starting at the third element.
temperatures.drop_while {|temperature| temperature < 30}
=> [30, 22, 27]

# The block returns false for the first element,
# so drop_while returns an array like the original,
# starting at the first element
# (in other words, a copy of the original).
temperatures.drop_while {|temperature| temperature < 28}
=> [28, 25, 30, 22, 27]
</pre>
<p></code></p>
<h3>Enumerable#drop_while and Hashes</h3>
<p>When used on a hash, <code>drop_while</code> effectively:</p>
<ul>
<li>Creates an array based on the hash, with each element in the hash represented as a two-element array where the first element contains the key and the second element containing the corresponding value, then</li>
<li>goes through each element in the array, dropping elements until it encounters the first element that doesn&#8217;t meet the condition in the block. The resulting array is an array of two-element arrays, starting at the first element that doesn&#8217;t meet the condition in the block.</li>
</ul>
<p>Once again, examples will make this all clear:</p>
<p><code>
<pre>
# We're basically taking the array from the previous example
# and dressing it up in a hash
city_temperatures = {"New York" => 28, \
                     "Toronto" => 25, \
                     "Washington" => 30, \
                     "Montreal" => 22, \
                     "Boston" => 27}
=> {"New York"=>28, "Toronto"=>25, "Washington"=>30,
"Montreal"=>22, "Boston"=>27}

# The block returns true for the FIRST two elements,
# and false for the third.
# So drop_while returns an array based on the hash,
# but starting at the third element
# (and, of course, the key-value pairs turned into
# two-element arrays).
city_temperatures.drop_while {|city_temperature| city_temperature[1] < 30}
=> [["Washington", 30], ["Montreal", 22], ["Boston", 27]]

# This is a more readable version of the line above
city_temperatures.drop_while {|city, temperature| temperature < 30}
=> [["Washington", 30], ["Montreal", 22], ["Boston", 27]]

# The block returns false for the first element,
# so drop_while returns an array based on the hash,
# starting at the first element
# (in other words, an array based on the original hash,
# with key-value pairs turned into two-element arrays).
city_temperatures.drop_while {|city,temperature| temperature < 28}
=> [["New York", 28], ["Toronto", 25], ["Washington", 30],
["Montreal", 22], ["Boston", 27]]
</pre>
<p></code></p>
<h3>Enumerable#drop_while&#8217;s Evil Twin, Enumerable#take_while</h3>
<p>I’ll cover <code>take_while</code> in detail in a later installment, but for now, an example should suffice:</p>
<p><code>
<pre>
city_temperatures.drop_while {|city_temperature| city_temperature[1] < 30}
=> [["Washington", 30], ["Montreal", 22], ["Boston", 27]]

city_temperatures.take_while {|city_temperature| city_temperature[1] < 30}
=> [["New York", 28], ["Toronto", 25]]
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.globalnerdy.com/2008/07/25/enumerating-enumerable-enumerabledrop_while/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
