<?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; first</title>
	<atom:link href="http://www.globalnerdy.com/tag/first/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#first</title>
		<link>http://www.globalnerdy.com/2008/08/15/enumerating-enumerable-enumerablefirst/</link>
		<comments>http://www.globalnerdy.com/2008/08/15/enumerating-enumerable-enumerablefirst/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 04:39:56 +0000</pubDate>
		<dc:creator>Joey deVilla</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Enumerating Enumerable]]></category>
		<category><![CDATA[first]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.globalnerdy.com/?p=1238</guid>
		<description><![CDATA[
Welcome to another installment of Enumerating Enumerable, my series of articles in I attempt to do a better job of documenting Ruby&#8217;s Enumerable module than Ruby-Doc.org. In this installment, I cover the first method.
In case you missed any of the previous articles, they&#8217;re listed and linked below:

all?
any?
collect / map
count
cycle
detect / find
drop
drop_while
each_cons
each_slice
each_with_index
entries / to_a
find_all / select
find_index

Enumerable#first [...]]]></description>
			<content:encoded><![CDATA[<p></p><p class="center"><img src="http://globalnerdy.com/wordpress/wp-content/uploads/2008/06/enumerating_enumerable.jpg" alt="Enumerating Enumerable" title="enumerating_enumerable" width="350" height="120" /></p>
<p>Welcome to another installment of <strong><cite>Enumerating Enumerable</cite></strong>, my series of articles in I attempt to do a better job of documenting Ruby&#8217;s <code>Enumerable</code> module than Ruby-Doc.org. In this installment, I cover the <code>first</code> method.</p>
<p>In case you missed any of the previous articles, 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>
<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>
<li><a href="http://globalnerdy.com/2008/07/25/enumerating-enumerable-enumerabledrop_while/">drop_while</a></li>
<li><a href="http://globalnerdy.com/2008/07/28/enumerating-enumerable-enumerableeach_cons/">each_cons</a></li>
<li><a href="http://www.globalnerdy.com/2008/07/29/enumerating-enumerable-enumerableeach_slice/">each_slice</a></li>
<li><a href="http://www.globalnerdy.com/2008/08/01/enumerating-enumerable-enumerableeach_with_index/">each_with_index</a></li>
<li><a href="http://www.globalnerdy.com/2008/08/02/enumerating-enumerable-enumerableentries-enumerableto_a/">entries / to_a</a></li>
<li><a href="http://www.globalnerdy.com/2008/08/07/enumerating-enumerable-enumerablefind_all-enumerableselect/">find_all / select</a></li>
<li><a href="http://www.globalnerdy.com/2008/08/14/enumerating-enumerable-enumerablefind_index/">find_index</a></li>
</ol>
<h3>Enumerable#first Quick Summary</h3>
<p class="center"><img src="http://www.globalnerdy.com/wordpress/wp-content/uploads/2008/08/ruby_enumerablefirst.jpg" alt="Graphic representing the &quot;first&quot; method in Ruby&#039;s &quot;Enumerable&quot; module" title="Graphic representing the &quot;first&quot; method in Ruby&#039;s &quot;Enumerable&quot; module" width="329" height="244" /></p>
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th>In the simplest possible terms</th>
<td>What are the first <em>n</em> items in the collection?</td>
</tr>
<tr>
<th>Ruby version</th>
<td>1.8 and 1.9</td>
</tr>
<tr>
<th>Expects</th>
<td>An optional integer <em>n</em> that specifies the first <em>n</em> items of the collection to return. If this integer is not given, <em>n</em> is 1 by default.</td>
</tr>
<tr>
<th>Returns</th>
<td>If <code>first</code> is applied to a collection containing <em>m</em> elements:</p>
<ul>
<li>The first item in the collection, if <em>m</em> > 0 and no argument <em>n</em> is provided.</li>
<li>An array containing the first <em>n</em> items in the collection, if <em>m</em> > 0 and an argument <em>n</em> is provided.</li>
<li><code>nil</code> if the collection is empty and no argument <em>n</em> is provided.</li>
<li>The empty array <code>[]</code> if the collection is empty and an argument <em>n</em> is provided.</li>
</ul>
</td>
</tr>
<tr>
<th>RubyDoc.org&#8217;s entry</th>
<td><a href="http://www.ruby-doc.org/core-1.9/classes/Enumerable.html#M002975">Enumerable#first</a></td>
</tr>
</table>
<h3>Enumerable#first and Arrays</h3>
<p>When used on an array without an argument, <code>first</code> returns the first item in the array:<br />
<code>
<pre>
posts = ["First post!", "Second post!", "Third post!"]
=> ["First post!", "Second post!", "Third post!"]

# What's the first item in posts?
posts.first
=> "First post!"

# Here's the equivalent using array notation:
posts[0]
=> "First post!"
</pre>
<p></code></p>
<p>When used on an array with an integer argument <em>n</em>, <code>first</code> returns an array containing the first <em>n</em> items in the original array:<br />
<code>
<pre>
# What are the first 2 items in posts?
posts.first 2
=> ["First post!", "Second post!"]

# Note that when you provide an argument of 1,
# the result is still an array -- with just one element.
# If you want a scalar, don't use an argument.
posts.first 1
=> ["First post!"]

# Here's the equivalent using array slice notation:
posts[0..1]
=> ["First post!", "Second post!"]

posts[0...2]
=> ["First post!", "Second post!"]
</pre>
<p></code></p>
<p>When used on an empty array, <code>first</code> returns:</p>
<ul>
<li><code>nil</code> if no argument <em>n</em> is provided</li>
<li>The empty array, <code>[]</code>, if an argument <em>n</em> is provided</li>
</ul>
<p><code>
<pre>
[].first
=> nil

[].first 2
=> []
</pre>
<p></code></p>
<h3>Enumerable#first and Hashes</h3>
<p>In Ruby 1.8 and previous versions, hash order is seemingly arbitrary. Starting with Ruby 1.9, hashes retain the order in which they were defined, which makes the <code>first</code> method a little more applicable.</p>
<p>When used on a hash without an argument, <code>first</code> returns the first item in the hash as a two-element array, with the key as the first element and the corresponding value as the second element.<br />
<code>
<pre>
# Let's see what stages of partying our friends are in
party_stages = {"Alice" => :party_mineral,
                "Bob"   => :party_animal,
                "Carol" => :party_reptile,
                "Dave"  => :party_vegetable}
=> {"Alice"=>:party_mineral, "Bob"=>:party_animal, "Carol"=>:party_reptile, "Dave"=>:party_vegetable}

# Who's the first partier and what state is s/he in?
party_stages.first
=> ["Alice", :party_mineral]
</pre>
<p></code></p>
<p>When used on a hash with an integer argument <em>n</em>, <code>first</code> returns an array containing the first <em>n</em> items in the hash, with each item represented as a two-element array:<br />
<code>
<pre>
party_stages.first 2
=> [["Alice", :party_mineral], ["Bob", :party_animal]]

# Note that when you provide an argument of 1,
# the result is still an array -- with just one element.
# If you want a scalar, don't use an argument.
party_stages.first 1
=> [["Alice", :party_mineral]]
</pre>
<p></code></p>
<p>When used on an empty hash, <code>first</code> returns:</p>
<ul>
<li><code>nil</code> if no argument <em>n</em> is provided</li>
<li>The empty array, <code>[]</code>, if an argument <em>n</em> is provided</li>
</ul>
<p><code>
<pre>
{}.first
=> nil

{}.first 2
=> []
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.globalnerdy.com/2008/08/15/enumerating-enumerable-enumerablefirst/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
