<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>gayleforce &#187; ruby</title>
	<atom:link href="http://gayleforce.wordpress.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://gayleforce.wordpress.com</link>
	<description>This Blog Doesn't Blow</description>
	<lastBuildDate>Fri, 04 Dec 2009 03:50:57 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='gayleforce.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/00eb25487f802f949b8e2b8888daa910?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>gayleforce &#187; ruby</title>
		<link>http://gayleforce.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://gayleforce.wordpress.com/osd.xml" title="gayleforce" />
		<item>
		<title>Model &#8211; View &#8211; Controller (MVC). Where do you put your business logic?</title>
		<link>http://gayleforce.wordpress.com/2009/11/28/model-view-controller-mvc-where-do-you-put-your-business-logic/</link>
		<comments>http://gayleforce.wordpress.com/2009/11/28/model-view-controller-mvc-where-do-you-put-your-business-logic/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 03:39:00 +0000</pubDate>
		<dc:creator>Gayle</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gayleforce.wordpress.com/2009/11/28/model-view-controller-mvc-where-do-you-put-your-business-logic/</guid>
		<description><![CDATA[When I first started doing Rails development over a year ago, I often found other developers suggesting that I refactor my code to move the business logic out of the controller, and into the model.&#160; After this happened several times, I wondered why this seemed so obvious to them, and why that wasn&#8217;t the way [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=227&subd=gayleforce&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>When I first started doing Rails development over a year ago, I often found other developers suggesting that I refactor my code to move the business logic out of the controller, and into the model.&#160; After this happened several times, I wondered why this seemed so obvious to them, and why that wasn&#8217;t the way I was naturally doing things.&#160; It wasn&#8217;t just me and another developer approaching problems in different ways.&#160; It was multiple people approaching problems in a similar way &#8212; and then me.&#160; I wondered why there was this general difference in thought processes and solutions? </p>
<p>I began to passively wonder if my background in Java was contributing in some way to my way of thinking here.&#160; Since then, I have also heard other Rails developers who are working with former Java developers say things like </p>
<blockquote><p><font size="3"><em><font color="#808080">&quot;these are good developers but they keep putting business logic in their controllers!&quot;</font></em>         <br /></font></p>
</blockquote>
<p>That made me think more seriously about what a person’s Java background might have to do with this. </p>
<p><font size="4">VIEW</font>&#160; </p>
<p>In earlier days of PHP, classic ASP, and early JSP, it was OK to put business logic in the view.&#160; Back in the day, it was common to find &lt;% %&gt; tags in JSPs with all kinds of logic in the Java code.&#160; I think we all now agree that business logic does not belong here.&#160; </p>
<blockquote><p><em><font color="#808080" size="3">let the view deal only with DISPLAYING what it has.</font></em></p>
</blockquote>
<p><font size="4">MODEL</font>&#160;</p>
<p>It seems like I&#8217;ve spent a lot of time seeing a model as simply a data-holder class.&#160; Something like a Struct in C.&#160; Something like a Bean in Java, with private attributes, and with get/set methods for each attribute.&#160; It&#8217;s just a data transfer object, nothing more.&#160; But why?&#160; Perhaps it comes from my EJB days.&#160; That might explain why so many other former Java developers do the same thing.&#160; To us,</p>
<blockquote><p><font size="3"><font color="#808080"><em><strike>the model carries data but is dumb.</strike></em></font>&#160;</font></p>
</blockquote>
<p><font size="4">CONTROLLER</font>&#160;</p>
<p>I always thought of the controller as being the one smacking around the model objects to get them to do what the controller wants – and </p>
<blockquote><p><font color="#808080" size="3"><em><strike>what the controller wants the model to do, is the business logic.</strike></em></font>&#160;</p>
</blockquote>
<p><font size="4">A RAILS WAY OF THINKING</font>&#160;</p>
<blockquote><p><font size="3"><em>Give the model more credit.</em></font></p>
</blockquote>
<p>The model isn&#8217;t so dumb after all.&#160; It doesn&#8217;t just hold state.&#160; It contains the business logic behind the <em>behavior</em> of that model.&#160; A model knows better than any other code how itself behaves, what it does or does not do, and how it interacts with the outside world.&#160; I can try to predict how my brother will behave in a given situation; I can take a guess based on what I know about him.&#160; But only he knows for sure, and the only way I can know for sure is to interact directly with him and find out. </p>
<blockquote><p><em><font size="3">Let the controller control the flow of information.</font></em></p>
</blockquote>
<p>This leaves the controller with only the responsibility of collecting information and routing it to the right place.&#160; It&#8217;s the electronic curator that finds and obtains art, and hands it off to the decorators to display.&#160; He decides what pieces go in which rooms.&#160; However, the decorators (analogous to the view) are the ones that decide how to arrange the art within that room for all to see. </p>
<p>I still need to be reminded sometimes to not put so much logic in my controller.&#160; It is starting to come more naturally to me, but old habits die hard I suppose.&#160; This way of thinking makes more sense to me now that I understand where my past way of thinking comes from.&#160; I like this new way because it really makes sense now, and also because</p>
<blockquote><p><em><font size="3">I find that code re-use comes much easier when needed business logic is already available in the model.</font></em></p>
</blockquote>
<p><font size="1">.</font></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gayleforce.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gayleforce.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gayleforce.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gayleforce.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gayleforce.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gayleforce.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gayleforce.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gayleforce.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gayleforce.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gayleforce.wordpress.com/227/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=227&subd=gayleforce&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://gayleforce.wordpress.com/2009/11/28/model-view-controller-mvc-where-do-you-put-your-business-logic/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e7c5690574dfe0863e1509de65c3f8ee?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">gayle9</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby Sorting [2] &#8211; Common Mistakes When Sorting With Blocks</title>
		<link>http://gayleforce.wordpress.com/2009/09/30/ruby-sorting-common-mistakes-when-sorting-with-blocks/</link>
		<comments>http://gayleforce.wordpress.com/2009/09/30/ruby-sorting-common-mistakes-when-sorting-with-blocks/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 14:00:54 +0000</pubDate>
		<dc:creator>Gayle</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gayleforce.wordpress.com/?p=175</guid>
		<description><![CDATA[This sorting technique is one I&#8217;ve had a chance to use at work more lately. But what keeps tripping me up is when you use the block to sorting primarily by one field, with a secondary sort on another field. Let&#8217;s say Fish has species and type, and we have these fish in our database:



species
type


Platy
Sunset


Platy
Calico


Molly
Dalmation


Platy
Rainbow


Guppy
Fancy [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=175&subd=gayleforce&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This sorting technique is one I&#8217;ve had a chance to use at work more lately. But what keeps tripping me up is when you use the block to sorting primarily by one field, with a secondary sort on another field. Let&#8217;s say Fish has species and type, and we have these fish in our database:</p>
<table style="margin-left:30px;margin-bottom:2px;border:1px solid;" border="1" cellspacing="0">
<tbody>
<tr>
<th>species</th>
<th>type</th>
</tr>
<tr>
<td>Platy</td>
<td>Sunset</td>
</tr>
<tr>
<td>Platy</td>
<td>Calico</td>
</tr>
<tr>
<td>Molly</td>
<td>Dalmation</td>
</tr>
<tr>
<td>Platy</td>
<td>Rainbow</td>
</tr>
<tr>
<td>Guppy</td>
<td>Fancy Tail</td>
</tr>
<tr>
<td>Platy</td>
<td>Mickey Mouse</td>
</tr>
</tbody>
</table>
<p>When I sort first by species, then by type, I keep accidentally doing the following, which gives the wrong sorted results:</p>
<pre style="padding-left:30px;">&gt;&gt; fishes = Fish.find(:all) 

&gt;&gt; fishes.sort do |a,b|
?&gt;   a.species &lt;=&gt; b.species
&gt;&gt;   a.type &lt;=&gt; b.type
&gt;&gt; end</pre>
<p>Doing that, I end up with a list like:</p>
<table style="margin-left:30px;margin-bottom:2px;border:1px solid;" border="1" cellspacing="0">
<tbody>
<tr>
<th>species</th>
<th>type</th>
</tr>
<tr>
<td>Platy</td>
<td>Calico</td>
</tr>
<tr>
<td>Molly</td>
<td>Dalmation</td>
</tr>
<tr>
<td>Guppy</td>
<td>Fancy Tail</td>
</tr>
<tr>
<td>Platy</td>
<td>Mickey Mouse</td>
</tr>
<tr>
<td>Platy</td>
<td>Rainbow</td>
</tr>
<tr>
<td>Platy</td>
<td>Sunset</td>
</tr>
</tbody>
</table>
<p>It ignores my first sort on species, and ends up sorting only by type!</p>
<p>Why? What&#8217;s wrong with that?  Well, the &lt;=&gt; comparator function (also known informally as the &#8220;spaceship operator&#8221;) returns either -1, 0 or 1, depending on whether the first value is less than, equal to, or greater than the other.  The block will return the last statement evaluated.  So what happens is we compare species, then we then compare type, and it is always the result of the type comparison, -1, 0 or 1, is returned from the block.  The problem is, if species <em>is not equal</em>, then we want to stop there and return -1 or 1 accordingly and not evaluate type at all.</p>
<p>A simple way to do this is to add &#8220;if result==0&#8243; to the end of the type comparison, and only evaluate type if species was equal.</p>
<pre style="padding-left:30px;">&gt;&gt; fishes = Fish.find(:all) 

&gt;&gt; fishes.sort do |a,b|
?&gt;   <strong><span style="color:#0000ff;">result = </span></strong>a.species &lt;=&gt; b.species
&gt;&gt;   <strong><span style="color:#0000ff;">result = </span></strong>a.type &lt;=&gt; b.type <span style="color:#0000ff;"><strong>if result == 0 </strong></span>
&gt;&gt;   <span style="color:#0000ff;"><strong>result</strong></span>
&gt;&gt; end</pre>
<p>This way, it will perform the first search by species, then only continue to perform the secondary search if the result of the first search was zero, that is they were equal values.  And so I end up with a list like:</p>
<table style="margin-left:30px;margin-bottom:2px;border:1px solid;" border="1" cellspacing="0">
<tbody>
<tr>
<th>species</th>
<th>type</th>
</tr>
<tr>
<td>Guppy</td>
<td>Fancy Tail</td>
</tr>
<tr>
<td>Molly</td>
<td>Dalmation</td>
</tr>
<tr>
<td>Platy</td>
<td>Calico</td>
</tr>
<tr>
<td>Platy</td>
<td>Mickey Mouse</td>
</tr>
<tr>
<td>Platy</td>
<td>Rainbow</td>
</tr>
<tr>
<td>Platy</td>
<td>Sunset</td>
</tr>
</tbody>
</table>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gayleforce.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gayleforce.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gayleforce.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gayleforce.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gayleforce.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gayleforce.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gayleforce.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gayleforce.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gayleforce.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gayleforce.wordpress.com/175/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=175&subd=gayleforce&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://gayleforce.wordpress.com/2009/09/30/ruby-sorting-common-mistakes-when-sorting-with-blocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e7c5690574dfe0863e1509de65c3f8ee?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">gayle9</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby Sorting [1] &#8211; When and Why to use sort_by()</title>
		<link>http://gayleforce.wordpress.com/2009/09/28/ruby-sorting-1-when-and-why-to-use-sort_by/</link>
		<comments>http://gayleforce.wordpress.com/2009/09/28/ruby-sorting-1-when-and-why-to-use-sort_by/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 14:00:26 +0000</pubDate>
		<dc:creator>Gayle</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gayleforce.wordpress.com/?p=171</guid>
		<description><![CDATA[When I read the rdoc on sort_by, I understood the general idea that sort_by is more efficient in some situations.  The specifics on why were still over my head, so I wasn&#8217;t planning to get into specifics during my recent talk on sorting. Yet just a few hours before my talk Jim Weirich was [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=171&subd=gayleforce&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>When I read <a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003120">the </a><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003120">rdoc on sort_by</a>, I understood the general idea that sort_by is more efficient in some situations.  The specifics on why were still over my head, so I wasn&#8217;t planning to get into specifics during my <a href="http://gayleforce.wordpress.com/2009/09/24/more-on-sorting-in-ruby/">recent talk on sorting</a>. Yet just a few hours before my talk <a href="http://onestepback.org/">Jim Weirich</a> was still trying to cajole me into using big words like &#8220;Schwartzian Transformation&#8221; in my talk because, he teased, &#8220;using big words makes you sound important <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &#8220;</p>
<p>The good thing is that this gave me a chance to talk it out with him, and actually understand it for real.  It was too late for me to add that into my talk a few hours before I was to give it, but I do want to talk about it here now that I understand.</p>
<p><strong>sort_by() is good if the values you&#8217;re sorting on require some kind of complex calculation or operation to get their value. </strong></p>
<p>Let&#8217;s say you have an aquarium, and you save the dates of when each fish is born in a database.  Later, you want to sort the list of fish by age.  But you must calculate the age based on the birth date.  So the Fish class has an age method:</p>
<pre style="padding-left:30px;">class Fish
...
  def age
    (Date.today - birthday).to_i
  end
end</pre>
<p>So when you sort like this:</p>
<pre style="padding-left:30px;">&gt;&gt; fishes = Fish.find(:all)

&gt;&gt; fishes.sort do |a, b|
&gt;&gt;   a.age &lt;=&gt; b.age
&gt;&gt; end</pre>
<p>It will calculate age over and over as it sorts.  And if you&#8217;ve studied sorting algorithms, you know that the items in the list are compared with other list items repeatedly until it can be determined where the items go in the ordered list.  So using this way of sorting, the age will be calculated a lot!</p>
<p>When you use sort_by() instead:</p>
<pre style="padding-left:30px;">&gt;&gt; fishes.sort_by do |a|
&gt;&gt;   a.age
&gt;&gt; end</pre>
<p>It does 3 things:</p>
<p>1. It will first go through each item in fishes, calculate age, and put those values into a temporary array keyed by the value.  Let&#8217;s say we have 3 fish, one 300 days old, one 365 days old, and one 225 days old.  The temporary array looks like this</p>
<pre style="padding-left:30px;">[[300, #&lt;Fish:A&gt;][365, #&lt;Fish:B&gt;][225, #&lt;Fish:C&gt;]</pre>
<p>2. The complex calculation is now done, once for each fish. It sorts this temporary array by the first item in each sub array.  Meaning, it sorts by the numbers 300, 365 and 225, without recalculating them.</p>
<pre style="padding-left:30px;">[[225, #&lt;Fish:C&gt;],[300, #&lt;Fish:A&gt;][365, #&lt;Fish:B&gt;]]</pre>
<p>3. Lastly, it goes back through the array, grabbing the 2nd array elements (the actual Fish objects) and putting them in order into a flattened 1-dimensional array</p>
<pre style="padding-left:30px;">[#&lt;Fish:C&gt;, #&lt;Fish:A&gt;, #&lt;Fish:B&gt;]
</pre>
<p>So, that is how you end up with a sorted array without recalculating values more than you need to. And that is why sort_by() can be more efficient.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gayleforce.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gayleforce.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gayleforce.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gayleforce.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gayleforce.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gayleforce.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gayleforce.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gayleforce.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gayleforce.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gayleforce.wordpress.com/171/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=171&subd=gayleforce&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://gayleforce.wordpress.com/2009/09/28/ruby-sorting-1-when-and-why-to-use-sort_by/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e7c5690574dfe0863e1509de65c3f8ee?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">gayle9</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby Sorting [0] &#8211; Sorting a Hash</title>
		<link>http://gayleforce.wordpress.com/2009/09/25/ruby-sorting-0-sorting-a-hash/</link>
		<comments>http://gayleforce.wordpress.com/2009/09/25/ruby-sorting-0-sorting-a-hash/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 14:00:59 +0000</pubDate>
		<dc:creator>Gayle</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gayleforce.wordpress.com/?p=166</guid>
		<description><![CDATA[It figures that one of the questions someone asked, following my talk on sorting last month, was something I specifically chose not to cover for the sake of time, and to be able to cover more valuable topics.   So, when someone asked whether you can sort a Hash and how, I knew A) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=166&subd=gayleforce&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>It figures that one of the questions someone asked, following <a href="&lt;a href=&quot;/2009/08/17/speaking-at-the-columbus-ruby-brigade/&quot;&gt;">my talk on sorting last month</a>, was something I specifically chose not to cover for the sake of time, and to be able to cover more valuable topics.   So, when someone asked whether you can sort a Hash and how, I knew A) that you can, and B) I knew I had barely glanced at it in the <a href="http://www.ruby-doc.org/core/classes/Hash.html#M002865">rdoc on hash sorting</a>, but didn&#8217;t remember how it worked.  So I had to simply suggest they go read about it themselves. But I was also curious to go read about it more myself.  So I took my own advice, and here&#8217;s what I came up with:</p>
<p><strong>I define a hash:</strong></p>
<pre style="padding-left:30px;">irb(main):001:0&gt; typical_fish_colors =
irb(main):002:0*    {"clownfish" =&gt; ["orange","white","black"],
irb(main):003:1*     "goldfish" =&gt; ["orange"],
irb(main):004:1*     "angelfish" =&gt; ["black","white"]
irb(main):005:1&gt;    }</pre>
<p><strong>Default sorting on a hash &#8211; sorts the keys. </strong></p>
<p>Pretty simple I guess. What you may not expect, depending on what you know of hashes, is that <span style="text-decoration:underline;">you don&#8217;t get a Hash object returned from calling .sort</span>, you get an Array.  A nested, three-dimensional Array.  This is because arrays are ordered, hashes are not.</p>
<pre style="padding-left:30px;">irb(main):006:0&gt; typical_fish_colors.sort
=&gt; [["angelfish", ["black", "white"]], ["clownfish", ["orange", "white", "black"]],
   ["goldfish", ["orange"]]]</pre>
<p><strong>What you can&#8217;t do when sorting a hash is use sort! (sort-bang):</strong></p>
<pre style="padding-left:30px;">irb(main):007:0&gt; typical_fish_colors.sort!
NoMethodError: undefined method `sort!' for #&lt;Hash:0x2d17ea8&gt;
 from (irb):7</pre>
<p><strong>What else you can&#8217;t do when sorting a hash <em>by default </em>is use symbols for keys:</strong></p>
<pre style="padding-left:30px;">irb(main):008:0&gt; typical_fish_colors =
irb(main):009:0*     {:clownfish =&gt; ["orange","white","black"],
irb(main):010:1*      :goldfish =&gt; ["orange"],
irb(main):011:1*      :angelfish =&gt; ["black","white"]
irb(main):012:1&gt;     }

irb(main):013:0&gt; typical_fish_colors.sort
NoMethodError: undefined method `&lt;=&gt;' for :clownfish:Symbol
 from (irb):13:in `&lt;=&gt;'
 from (irb):13:in `sort'
 from (irb):13
</pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gayleforce.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gayleforce.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gayleforce.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gayleforce.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gayleforce.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gayleforce.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gayleforce.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gayleforce.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gayleforce.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gayleforce.wordpress.com/166/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=166&subd=gayleforce&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://gayleforce.wordpress.com/2009/09/25/ruby-sorting-0-sorting-a-hash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e7c5690574dfe0863e1509de65c3f8ee?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">gayle9</media:title>
		</media:content>
	</item>
		<item>
		<title>On Sorting in Ruby</title>
		<link>http://gayleforce.wordpress.com/2009/09/24/on-sorting-in-ruby/</link>
		<comments>http://gayleforce.wordpress.com/2009/09/24/on-sorting-in-ruby/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 04:38:20 +0000</pubDate>
		<dc:creator>Gayle</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gayleforce.wordpress.com/?p=104</guid>
		<description><![CDATA[After speaking at the Columbus Ruby Brigade last month on the topic of Ruby sorting, I&#8217;ve gotten a chance to use some of the sorting techniques I spoke about more over the last month at work.  There were a few things I didn&#8217;t get a chance to cover since I was only giving a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=104&subd=gayleforce&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>After <a href="/2009/08/17/speaking-at-the-columbus-ruby-brigade/">speaking at the Columbus Ruby Brigade last month</a> on the topic of Ruby sorting, I&#8217;ve gotten a chance to use some of the sorting techniques I spoke about more over the last month at work.  There were a few things I didn&#8217;t get a chance to cover since I was only giving a 5-10 minute lightning talk. There&#8217;s a few things I&#8217;ve come to understand a little better (benefits and gotcha&#8217;s) since then.</p>
<p><strong>Things I talked about (feel free to check out <a href="http://docs.google.com/present/view?id=dwpcqwz_0gzwsnggs">the slides from my talk</a> for examples of some of these things) </strong></p>
<ul>
<li>Basic sorting with .sort and .sort!</li>
<li>Sorting your own complex types by defining a &lt;=&gt; method on your class</li>
<li>Sorting on the fly with blocks</li>
<li>Sorting nested objects</li>
</ul>
<p><strong>Things I did not cover for various reasons. And blog posts covering them in more detail after the fact:</strong></p>
<ul>
<li><a href="/2009/09/25/ruby-sorting-0-sorting-a-hash/">Sorting a Hash</a> &#8211; covering this topic didn&#8217;t seem as if it would add as much value as some of the other things I covered, so it got cut for the sake of time.</li>
<li><a href="http://gayleforce.wordpress.com/2009/09/28/ruby-sorting-1-when-and-why-to-use-sort_by/">When and specifically why to use sort_by</a> &#8211; because I didn&#8217;t fully understand it just reading the <a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003120">rdoc on sort_by </a>but now I do.</li>
<li><a href="http://gayleforce.wordpress.com/2009/09/30/ruby-sorting-common-mistakes-when-sorting-with-blocks/">Common mistakes when sorting using blocks</a> &#8211; things I&#8217;ve learned along the way, in practice, since last month.</li>
</ul>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gayleforce.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gayleforce.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gayleforce.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gayleforce.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gayleforce.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gayleforce.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gayleforce.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gayleforce.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gayleforce.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gayleforce.wordpress.com/104/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=104&subd=gayleforce&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://gayleforce.wordpress.com/2009/09/24/on-sorting-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e7c5690574dfe0863e1509de65c3f8ee?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">gayle9</media:title>
		</media:content>
	</item>
		<item>
		<title>Speaking at the Columbus Ruby Brigade</title>
		<link>http://gayleforce.wordpress.com/2009/08/17/speaking-at-the-columbus-ruby-brigade/</link>
		<comments>http://gayleforce.wordpress.com/2009/08/17/speaking-at-the-columbus-ruby-brigade/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 19:02:12 +0000</pubDate>
		<dc:creator>Gayle</dc:creator>
				<category><![CDATA[community]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gayleforce.wordpress.com/?p=89</guid>
		<description><![CDATA[Tonight I will be speaking at the Columbus Ruby Brigade (CRB) meeting.  I will be taking the lead from another CRB member, Kevin Munc, who has started a series of introductory talks focusing on taking one method in Ruby and doing a 5-10 minute lightning talking about that method.  &#8220;Ruby Method of the Month.&#8221;  Tonight [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=89&subd=gayleforce&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Tonight I will be speaking at the <a href="http://columbusrb.com">Columbus Ruby Brigade</a> (CRB) meeting.  I will be taking the lead from another CRB member, <a href="http://www.munc.com">Kevin Munc</a>, who has started a series of introductory talks focusing on taking one method in Ruby and doing a 5-10 minute lightning talking about that method.  &#8220;Ruby Method of the Month.&#8221;  Tonight I will be speaking about sorting in Ruby.</p>
<p>I will post a copy my slides up here soon.  Not until after I&#8217;m done so nobody cheats <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Update: August 18, 2009</strong><br />
Here&#8217;s the <a href="http://docs.google.com/present/view?id=dwpcqwz_0gzwsnggs">slides</a> from my talk, enjoy!</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gayleforce.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gayleforce.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gayleforce.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gayleforce.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gayleforce.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gayleforce.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gayleforce.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gayleforce.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gayleforce.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gayleforce.wordpress.com/89/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=89&subd=gayleforce&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://gayleforce.wordpress.com/2009/08/17/speaking-at-the-columbus-ruby-brigade/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e7c5690574dfe0863e1509de65c3f8ee?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">gayle9</media:title>
		</media:content>
	</item>
		<item>
		<title>Great Lakes Ruby Bash</title>
		<link>http://gayleforce.wordpress.com/2008/10/08/great-lakes-ruby-bash/</link>
		<comments>http://gayleforce.wordpress.com/2008/10/08/great-lakes-ruby-bash/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 22:56:35 +0000</pubDate>
		<dc:creator>Gayle</dc:creator>
				<category><![CDATA[community]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gayleforce.wordpress.com/?p=51</guid>
		<description><![CDATA[As I&#8217;ve mentioned before, I&#8217;ve enjoyed going beyond my local network of technical folks and attending regional events in Ohio and Michigan such as CodeMash, Cleveland Day of .Net (even though I&#8217;ve never been a .Net developer) and Agile Summer Camp. 
It&#8217;s high time I expand my Ruby-specific regional network, especially now that I&#8217;m getting paid [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=51&subd=gayleforce&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>As I&#8217;ve mentioned before, I&#8217;ve enjoyed going beyond my local network of technical folks and attending regional events in Ohio and Michigan such as <a href="http://codemash.org">CodeMash</a>, <a href="http://www.clevelanddodn.org/">Cleveland Day of .Net</a> (even though I&#8217;ve never been a .Net developer) and <a href="http://agilesummercamp.com/">Agile Summer Camp</a>. </p>
<p>It&#8217;s high time I expand my Ruby-specific regional network, especially now that I&#8217;m getting paid to write Ruby code! <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>This weekend, I&#8217;m looking forward to attending the<br />
<a href="http://greatlakesrubybash.org/"><img src="http://greatlakesrubybash.org/images/glrb_cropped.jpg" alt="Great Lakes Ruby Bash" /></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gayleforce.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gayleforce.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gayleforce.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gayleforce.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gayleforce.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gayleforce.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gayleforce.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gayleforce.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gayleforce.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gayleforce.wordpress.com/51/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=51&subd=gayleforce&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://gayleforce.wordpress.com/2008/10/08/great-lakes-ruby-bash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e7c5690574dfe0863e1509de65c3f8ee?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">gayle9</media:title>
		</media:content>

		<media:content url="http://greatlakesrubybash.org/images/glrb_cropped.jpg" medium="image">
			<media:title type="html">Great Lakes Ruby Bash</media:title>
		</media:content>
	</item>
		<item>
		<title>eRubyCon 2008</title>
		<link>http://gayleforce.wordpress.com/2008/08/12/erubycon-2008/</link>
		<comments>http://gayleforce.wordpress.com/2008/08/12/erubycon-2008/#comments</comments>
		<pubDate>Tue, 12 Aug 2008 21:32:50 +0000</pubDate>
		<dc:creator>Gayle</dc:creator>
				<category><![CDATA[community]]></category>
		<category><![CDATA[erubycon]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gayleforce.wordpress.com/?p=32</guid>
		<description><![CDATA[eRubyCon 2007 was a great time in Columbus Ohio last year, and I will be attending eRubyCon again this year!



       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=32&subd=gayleforce&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>eRubyCon 2007 was a great time in Columbus Ohio last year, and I will be attending eRubyCon again this year!<br />
<a href="http://erubycon.com"><br />
<img src="http://erubycon.com/images/banner_large_attending.png" alt="eRubyCon. August 15-17, 2008" /><br />
</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/gayleforce.wordpress.com/32/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/gayleforce.wordpress.com/32/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gayleforce.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gayleforce.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gayleforce.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gayleforce.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gayleforce.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gayleforce.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gayleforce.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gayleforce.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gayleforce.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gayleforce.wordpress.com/32/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=32&subd=gayleforce&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://gayleforce.wordpress.com/2008/08/12/erubycon-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e7c5690574dfe0863e1509de65c3f8ee?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">gayle9</media:title>
		</media:content>

		<media:content url="http://erubycon.com/images/banner_large_attending.png" medium="image">
			<media:title type="html">eRubyCon. August 15-17, 2008</media:title>
		</media:content>
	</item>
		<item>
		<title>My big move: from Java to Ruby</title>
		<link>http://gayleforce.wordpress.com/2008/06/09/my-big-move-from-java-to-ruby/</link>
		<comments>http://gayleforce.wordpress.com/2008/06/09/my-big-move-from-java-to-ruby/#comments</comments>
		<pubDate>Tue, 10 Jun 2008 04:42:49 +0000</pubDate>
		<dc:creator>Gayle</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gayleforce.wordpress.com/?p=14</guid>
		<description><![CDATA[I&#8217;m pleased to announce that effective 6/9/2008 I have accepted a Ruby/Rails job with MedWiz Technologies. I&#8217;m taking the leap from Java, which I&#8217;ve done for over 9 years now, into Ruby, which I&#8217;ve been interested in for over a year. I&#8217;m very excited, somewhat nervous, but very much looking forward to this new opportunity [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=14&subd=gayleforce&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I&#8217;m pleased to announce that effective 6/9/2008 I have accepted a Ruby/Rails job with <a href="http://www.medwiztech.com">MedWiz Technologies</a>. I&#8217;m taking the leap from Java, which I&#8217;ve done for over 9 years now, into Ruby, which I&#8217;ve been interested in for over a year. I&#8217;m very excited, somewhat nervous, but very much looking forward to this new opportunity and step in my career!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/gayleforce.wordpress.com/14/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/gayleforce.wordpress.com/14/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gayleforce.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gayleforce.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gayleforce.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gayleforce.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gayleforce.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gayleforce.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gayleforce.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gayleforce.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gayleforce.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gayleforce.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gayleforce.wordpress.com&blog=2524365&post=14&subd=gayleforce&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://gayleforce.wordpress.com/2008/06/09/my-big-move-from-java-to-ruby/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e7c5690574dfe0863e1509de65c3f8ee?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">gayle9</media:title>
		</media:content>
	</item>
	</channel>
</rss>