<?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; each_slice</title>
	<atom:link href="http://www.globalnerdy.com/tag/each_slice/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.globalnerdy.com</link>
	<description>Tech Evangelist Joey deVilla on Shopify, startups, software development, tech news and other nerdy stuff</description>
	<lastBuildDate>Thu, 09 Feb 2012 18:30:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Enumerating Enumerable: Enumerable#each_slice</title>
		<link>http://www.globalnerdy.com/2008/07/29/enumerating-enumerable-enumerableeach_slice/</link>
		<comments>http://www.globalnerdy.com/2008/07/29/enumerating-enumerable-enumerableeach_slice/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 05:23:48 +0000</pubDate>
		<dc:creator>Joey deVilla</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[each_slice]]></category>
		<category><![CDATA[enumerable]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://globalnerdy.com/?p=1938</guid>
		<description><![CDATA[<p style="text-align: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>Ten installments already? That's right, this is the tenth <strong><cite>Enumerating Enumerable</cite></strong> article. As I'm fond of repeating, this is my little contribution to the Ruby community: a series of articles where I attempt to do a better job at documenting Ruby's <code>Enumerable</code> module than <a href="http://www.ruby-doc.org/core-1.9/classes/Enumerable.html">Ruby-Doc.org does</a>, with pretty pictures and more in-depth examples!</p>

<p>In this article, I'm going to cover <code>each_slice</code>, which got introduced in Ruby 1.9.</p>

<p style="text-align:center;"><a href="http://globalnerdy.com/2008/07/29/enumerating-enumerable-enumerableeach_slice/"><img src="http://globalnerdy.com/wordpress/wp-content/uploads/2008/07/ruby_enumerableeach_slice.jpg" alt="Graphic representation of the &#34;each_slice&#34; method in Ruby&#039;s &#34;Enumerable&#34; module" title="Graphic representation of the &#34;each_slice&#34; method in Ruby&#039;s &#34;Enumerable&#34; module" width="325" height="371" /></a></p>

<p>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>
    <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>
</ol>

<p><a href="http://globalnerdy.com/2008/07/29/enumerating-enumerable-enumerableeach_slice/"><strong>Read on for more...</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="Enumerating Enumerable" title="enumerating_enumerable" width="350" height="120" /></p>
<p>Ten installments already? That&#8217;s right, this is the tenth <strong><cite>Enumerating Enumerable</cite></strong> article. As I&#8217;m fond of repeating, this is my little contribution to the Ruby community: a series of articles where I attempt to do a better job at documenting Ruby&#8217;s <code>Enumerable</code> module than <a href="http://www.ruby-doc.org/core-1.9/classes/Enumerable.html">Ruby-Doc.org does</a>, with pretty pictures and more in-depth examples!</p>
<p>In this article, I&#8217;m going to cover <code>each_slice</code>, which got introduced in Ruby 1.9.</p>
<p>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>
<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>
</ol>
<h3>Enumerable#each_slice Quick Summary</h3>
<p style="text-align:center;"><img src="http://globalnerdy.com/wordpress/wp-content/uploads/2008/07/ruby_enumerableeach_slice_2.jpg" alt="Graphic representation of the &quot;each_slice&quot; method in Ruby&#039;s &quot;Enumerable&quot; module" title="Graphic representation of the &quot;each_slice&quot; method in Ruby&#039;s &quot;Enumerable&quot; module" width="415" height="367" /></p>
<table>
<tr>
<th>In the simplest possible terms</th>
<td>Given a number <i>n</i>, split the array into <i>n</i>-element slices (if the number of elements in the array isn&#8217;t evenly divisible by <i>n</i>, just put the remaining elements in the last slice), then iterate through those slices.</td>
</tr>
<tr>
<th>Ruby version</th>
<td>1.9 only</td>
</tr>
<tr>
<th>Expects</th>
<td>
<ul>
<li>A number <i>n</i> describing the size of the iteration slice.</li>
<li>An optional block to receive the values from each iteration.</li>
</ul>
</td>
</tr>
<tr>
<th>Returns</th>
<td>
<ul>
<li><code>nil</code>, if used with a block.</li>
<li>An <code>Enumerator</code> object that outputs <i>n</i>-sized slices of the collection, if used without a block.</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#M002955">Enumerable#each_slice</a></td>
</tr>
</table>
<h3>Enumerable#each_slice and Arrays</h3>
<p>When used on an array with a block, <code>each_slice</code> takes a numeric argument <i>n</i> and splits the array into slices of length <i>n</i> (if the number of elements in the array isn&#8217;t evenly divisible by <i>n</i>, the remaining elements are put into the last slice). <code>each_slice</code> then iterates through the set of slices, passing each slice to the block. After the final iteration, <code>each_slice</code> returns <code>nil</code>.</p>
<p>Since this is yet another case when an showing an example makes things very clear, I&#8217;ll do just that, using the same example data I used in <a href="http://globalnerdy.com/2008/07/28/enumerating-enumerable-enumerableeach_cons/">the article on <code>each_cons</code></a>:</p>
<p><code>
<pre>
justice_league = ["Aquaman", "Batman", "Black Canary",
                  "Flash", "Green Arrow", "Green Lantern",
                  "Martian Manhunter", "Superman",
                  "Vixen", "Wonder Woman"]
justice_league = ["Aquaman", "Batman", "Black Canary", "Flash", "Green Arrow",
"Green Lantern", "Martian Manhunter", "Superman", "Vixen", "Wonder Woman"]

justice_league.each_slice(3) {|team| p team}
=> ["Aquaman", "Batman", "Black Canary"]
["Flash", "Green Arrow", "Green Lantern"]
["Martian Manhunter", "Superman", "Vixen"]
["Wonder Woman"]
=> nil
</pre>
<p></code></p>
<p>When used on an array without a block, <code>each_slice</code> takes a numeric argument <i>n</i> and returns an <code>Enumerator</code> that outputs slices of the array when its <code>next</code> method is called. Here&#8217;s an example:</p>
<p><code>
<pre>
teams_of_3 = justice_league.each_slice(3)
=> #&lt;Enumerable::Enumerator:0x007fc73baa9830&gt;

teams_of_3.next
=> ["Aquaman", "Batman", "Black Canary"]

teams_of_3.next
=> ["Flash", "Green Arrow", "Green Lantern"]

teams_of_3.next
=> ["Martian Manhunter", "Superman", "Vixen"]

teams_of_3.next
=> ["Wonder Woman"]

teams_of_3.next
=> StopIteration: iteration reached at end...
# (I'm skipping the rest of the error message)

teams_of_3.rewind
=> #&lt;Enumerable::Enumerator:0x007fc73baa9830&gt;

teams_of_3.next
=> ["Aquaman", "Batman", "Black Canary"]
</pre>
<p></code></p>
<h3>Enumerable#each_slice and Hashes</h3>
<p>When used on an hash with a block, <code>each_slice</code> takes a numeric argument <i>n</i> and splits the hash into arrays of length <i>n</i> (if the number of elements in the array isn&#8217;t evenly divisible by <i>n</i>, the remaining elements are put into the last slice). Within these arrays, each hash element is represented as a two-element array, with the first element being the key and the second element being the corresponding value.</p>
<p><code>each_slice</code> then iterates through the set of arrays, passing each array to the block. After the final iteration, <code>each_slice</code> returns <code>nil</code>.</p>
<p>Here&#8217;s an example, using the same example data I used in <a href="http://globalnerdy.com/2008/07/28/enumerating-enumerable-enumerableeach_cons/">the article on <code>each_cons</code></a>:</p>
<p><code>
<pre>
enterprise_crew = {:captain => "Picard",
                   :first_officer => "Riker",
                   :science_officer => "Data",
                   :tactical_officer => "Worf",
                   :chief_engineer => "LaForge",
                   :chief_medical_officer => "Crusher",
                   :ships_counselor => "Troi",
                   :annoying_ensign => "Crusher",
                   :attractive_ensign => "Ro",
                   :expendable_crew_member => "Smith"}
=> {:captain=>"Picard", :first_officer=>"Riker", :science_officer=>"Data",
:tactical_officer=>"Worf", :chief_engineer=>"LaForge",
:chief_medical_officer=>"Crusher", :ships_counselor=>"Troi",
:annoying_ensign=>"Crusher", :attractive_ensign=>"Ro",
:expendable_crew_member=>"Smith"}
=> nil

enterprise_crew.each_slice(3) {|team| p team}
=> [[:captain, "Picard"], [:first_officer, "Riker"], [:science_officer, "Data"]]
[[:tactical_officer, "Worf"], [:chief_engineer, "LaForge"],[:chief_medical_officer, "Crusher"]]
[[:ships_counselor, "Troi"], [:annoying_ensign, "Crusher"], [:attractive_ensign,  "Ro"]]
[[:expendable_crew_member, "Smith"]]
=> nil
</pre>
<p></code></p>
<p>When used on a hash without a block, <code>each_slice</code> takes a numeric argument <i>n</i> and returns an <code>Enumerator</code> that outputs arrays when its <code>next</code> method is called. Here&#8217;s an example:</p>
<p><code>
<pre>
away_teams_of_3 = enterprise_crew.each_slice(3)
=> #&lt;Enumerable::Enumerator:0x007fc73ba44c50&gt;

away_teams_of_3.next
=> [[:captain, "Picard"], [:first_officer, "Riker"], [:science_officer, "Data"]]

away_teams_of_3.next
=> [[:tactical_officer, "Worf"], [:chief_engineer, "LaForge"], [:chief_medical_officer, "Crusher"]]

away_teams_of_3.next
=> [[:ships_counselor, "Troi"], [:annoying_ensign, "Crusher"], [:attractive_ensign, "Ro"]]

away_teams_of_3.next
=> [[:expendable_crew_member, "Smith"]]

away_teams_of_3.next
=> StopIteration: iteration reached at end...
# (I'm skipping the rest of the error message)

away_teams_of_3.rewind
=> #<Enumerable::Enumerator:0x007fc73ba44c50>

away_teams_of_3.next
=> [[:captain, "Picard"], [:first_officer, "Riker"], [:science_officer, "Data"]]
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.globalnerdy.com/2008/07/29/enumerating-enumerable-enumerableeach_slice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

