<?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>matthewcarriere.com &#187; ruby</title>
	<atom:link href="http://matthewcarriere.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://matthewcarriere.com</link>
	<description>a blog about ruby on rails and the mac.</description>
	<lastBuildDate>Mon, 05 Jul 2010 21:29:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using select, reject, collect, inject and detect.</title>
		<link>http://matthewcarriere.com/2008/06/23/using-select-reject-collect-inject-and-detect/</link>
		<comments>http://matthewcarriere.com/2008/06/23/using-select-reject-collect-inject-and-detect/#comments</comments>
		<pubDate>Tue, 24 Jun 2008 06:08:16 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=26</guid>
		<description><![CDATA[I spend a lot of time convincing my friends to switch to a Mac. Some of my friends are also software developers so naturally, just when they think the evangelism has come to an end, I convince them to get on the Rails. However, learning Rails usually means learning Ruby for the first time as [...]]]></description>
			<content:encoded><![CDATA[<p>I spend a lot of time convincing my friends to switch to a Mac. Some of my friends are also software developers so naturally, just when they think the evangelism has come to an end, I convince them to get on the Rails. However, learning Rails usually means learning Ruby for the first time as well. In this post I am going to address one of the issues I see for newcomers to Ruby. Looping.</p>
<p>Looping in Ruby seems to be a process of evolution for newcomers to the language. Newcomers will always find their way to the for loop almost immediately and when confronted with iterating an array, the first choice will generally be a for..in:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">for</span> n <span style="color:#9966CC; font-weight:bold;">in</span> a
  <span style="color:#CC0066; font-weight:bold;">puts</span> n
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>This works, but its not very&#8230; Ruby. The next stage of evolution will be using an iterator for the first time. So the for loop gets dropped all together and <em>each</em> is used. The Rubyist is born at this point:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> n
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>What I see next is a lot of conditional logic being used inside the each block. The logic is generally introduced to perform the following operations:</p>
<ol>
<li>Building a list of items from an array.</li>
<li>Total the items in an array.</li>
<li>Find an item in the array.</li>
</ol>
<p>So if this is you, then stop. Ruby has plenty more iterators where <em>each</em> came from. Which one you should be using depends on what operation you are trying to perform. So let&#8217;s take a look at our previous list and see if we can find a more Ruby way to get them done.</p>
<h4>Building a list of items from the array using select</h4>
<p>For this operation you should be using <em>select</em>. The way select works is simple, it basically iterates through all the elements in your array and performs your logic on each one. If the logic returns TRUE, then it adds the item to a new array which it returns when the iteration is complete. Here&#8217;s an example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> n <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>This will return the last two elements in the array: 3 and 4. Why? Because 3 and 4 are both greater than 2, which was the logic we placed in the block. It&#8217;s worth noting that select has an evil step sister named <em>reject</em>. This will perform the opposite operation of select. Logic which returns FALSE adds the item to the array that is returned. Here&#8217;s the same examples as before except we will swap select, with reject:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#9900CC;">reject</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> n <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>In this example the return value is [1,2] because these elements return false when the condition is tested.</p>
<p>I also have to mention another close sibling to select and reject; collect, which returns an array of values that are the RESULT of logic in the block. Previously we returned the item based on the result of the CONDITION in the block. So perhaps we need square the values in our array:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> n<span style="color:#006600; font-weight:bold;">*</span>n<span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>This returns a new array with each item in our array squared.</p>
<p>Finally, note that using select, reject, and collect returns an array. If you want to return something different, because you are concatenating or totaling values, then check out inject.</p>
<h4>Total the items in an array using inject</h4>
<p>When you think of accumulating, concatenating, or totaling values in an array, then think of inject. The main difference between select and inject is that inject gives you another variable for use in the block. This variable, referred to as the accumulator, is used to store the running total or concatenation of each iteration. The value added to the accumulator is the result of the logic you place in the block. At the end of each iteration, whatever that value is, can be added to the accumulator. For example, let&#8217;s sum all the numbers together in our array:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#9900CC;">inject</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>acc,n<span style="color:#006600; font-weight:bold;">|</span> acc <span style="color:#006600; font-weight:bold;">+</span> n<span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>This will return 10. The total value of all the elements in our array. The logic in our block is simple: add the current element to the accumulator. Remember, you must do <em>something</em> to the accumulator in each iteration. If we had simply placed <em>n</em> in the block the final value of the accumulator would have been 4. Why? Because its the last value in the array and since we did not add it to the accumulator explicitly the accumulator would be replaced in each iteration.</p>
<p>You can also use a parameter with the inject call to determine what the default value for the accumulator is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>acc,n<span style="color:#006600; font-weight:bold;">|</span> acc <span style="color:#006600; font-weight:bold;">+</span> n<span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>In this example the result is 20 because we assigned the accumulator an initial value of 10.</p>
<p>If you need to return a string or an array from inject, then you will need to treat the accumulator variable that way. You can use the default value parameter of inject to do this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>acc,n<span style="color:#006600; font-weight:bold;">|</span> acc <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> n<span style="color:#006600; font-weight:bold;">+</span>n<span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>In this example I add n to itself and then append it to the accumulator variable. I initialized the accumulator as an empty array using the default value parameter.</p>
<h4>Find an item in the array using detect</h4>
<p>Our last example operation was to find an element in the array. Let&#8217;s just put it out there and say that other iterators could be used to <em>select</em> the correct value from the array, but I am going to show you how to use detect to round out our exploration of these iterators. </p>
<p>So let&#8217;s find the value 3 in our array using detect:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#9900CC;">detect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> n == <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>This returns 3. The value we were looking for. If the value had not been found, then the iterator returns <em>nil</em>.</p>
<p>So if your head is spinning at this point as to which iterator to use for when, then remember this:</p>
<ol>
<li>Use select or reject if you need to select or reject items based on a condition.</li>
<li>Use collect if you need to build an array of the results from logic in the block.</li>
<li>Use inject if you need to accumulate, total, or concatenate array values together.</li>
<li>Use detect if you need to find an item in an array.</li>
</ol>
<p>By using these iterators you will be one step closer to mastering&#8230; Ruby-Fu.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/06/23/using-select-reject-collect-inject-and-detect/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Take it to the next level with the Ruby on Rails console</title>
		<link>http://matthewcarriere.com/2008/01/27/take-it-to-the-next-level-with-the-ruby-on-rails-console/</link>
		<comments>http://matthewcarriere.com/2008/01/27/take-it-to-the-next-level-with-the-ruby-on-rails-console/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 05:53:19 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=14</guid>
		<description><![CDATA[If you are not using the Rails console while your developing then now is the time to start! I learn best by example, so that&#8217;s how I am going to structure this. Here&#8217;s the scenario: You are writing code against a class and need to determine which attributes you have available to you. In Rails, [...]]]></description>
			<content:encoded><![CDATA[<p>If you are not using the Rails console while your developing then now is the time to start! I learn best by example, so that&#8217;s how I am going to structure this.</p>
<p>Here&#8217;s the scenario:</p>
<p>You are writing code against a class and need to determine which attributes you have available to you. In Rails, your attributes are largely driven by the database schema, so you will not see them in the class definition. At this point most developers will take a look at the table in the database and find the column they need, then determine the type of data stored there. There is a better way&#8230;</p>
<p>Let&#8217;s use the command line.</p>
<p>We start by firing up our Rails application in console mode:<br />
<em>You must be in your Rails application directory to issue this command.</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">script<span style="color: #66cc66;">/</span>console</pre></td></tr></table></div>

<p>So now what? Well now you have command line access to your entire Rails application! For example, let&#8217;s say you have a class named Person (which would map to a people table in the database). You want a new instance of this class? Then issue the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> = Person.<span style="color:#9900CC;">new</span></pre></td></tr></table></div>

<p>Perhaps you want to view a Person from the database:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> = Person.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>By default the results are displayed below each command and they are not very friendly, so let&#8217;s output them in YAML as it will be much more readable:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span></pre></td></tr></table></div>

<p>&#8216;y&#8217; is like calling &#8216;puts p.to_yaml&#8217; on the resulting value that follows it. There is also a &#8216;p&#8217; shortcut that is the equivalent of issuing &#8216;puts&#8217; on the value.</p>
<p>So how does this help with determining attributes? At first glance you may think you have to look at the list of values returned in your YAML command and find what your looking for&#8230; no&#8230; it gets better. Remember that all Ruby objects inherit certain methods, watch this:</p>
<p>You want to get the methods associated with your object:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">methods</span></pre></td></tr></table></div>

<p>You can also get the private, public, and protected methods using the following commands:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">public_methods</span>
y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">private_methods</span>
y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">protected_methods</span></pre></td></tr></table></div>

<p>So this class could return a huge YAML result which may not seem as efficient as scanning the schema in the database, and you&#8217;re right. So let&#8217;s get to what we are really looking for using &#8216;grep&#8217;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">methods</span>.<span style="color:#9900CC;">grep</span> <span style="color:#006600; font-weight:bold;">/</span>method_name<span style="color:#006600; font-weight:bold;">/</span></pre></td></tr></table></div>

<p>This will return any method which contains <em>method_name</em>. You can write pass any regular expression you like into this command and really drill into your class methods, and for help putting those regular expressions together I really like using <a href="http://www.rubular.com/">Rubular</a> a web-based Ruby regular expression editor.</p>
<p>And what about the type of an object?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9966CC; font-weight:bold;">class</span></pre></td></tr></table></div>

<p>This is just a taste of what you can do with the console&#8230; I use the console everyday as I develop. I will post some more console tips in future posts!</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/01/27/take-it-to-the-next-level-with-the-ruby-on-rails-console/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running Ruby on Rails with Mongrel Clustering and Apache Proxy</title>
		<link>http://matthewcarriere.com/2008/01/18/running-ruby-on-rails-with-mongrel-clustering-and-apache-proxy/</link>
		<comments>http://matthewcarriere.com/2008/01/18/running-ruby-on-rails-with-mongrel-clustering-and-apache-proxy/#comments</comments>
		<pubDate>Sat, 19 Jan 2008 05:42:52 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[mongrel]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubygems]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=6</guid>
		<description><![CDATA[Being a Rails coder is easy, deploying a Rails application can be hard. Through a great deal of effort, we&#8217;ll call it blood, sweat and tears, I have put together the following guidance on how to get your Ruby on Rails application up and running behind a Mongrel cluster using Apache Proxy. Here&#8217;s the deal, [...]]]></description>
			<content:encoded><![CDATA[<p>Being a Rails coder is easy, deploying a Rails application can be hard. Through a great deal of effort, we&#8217;ll call it blood, sweat and tears, I have put together the following guidance on how to get your Ruby on Rails application up and running behind a Mongrel cluster using Apache Proxy.</p>
<p>Here&#8217;s the deal, I am going to install all the necessary applications to get this up and running, so if you have some of these installed on your server already you may just skip ahead, however, if you have any issues with this you may want to read over those sections you skipped and understand why I installed a specific version or what pitfalls I encountered that may be affecting your setup. Also, I am running Ubuntu, so if you are running a different distribution, then the paths to specific configuration files on your system may be different. Let&#8217;s get started!</p>
<h4>Installing MySQL</h4>
<p>Install MySQL using the package manager in Ubuntu:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo apt-get install mysql-<span style="color: #000066;">server</span></pre></td></tr></table></div>

<p>After the installation you should be able to run the command <em>mysql</em> or <em>mysqladmin</em>. If you cannot run these then you may have to create symbolic links to these commands or add them to your path.</p>
<p>By default, MySQL only listens on localhost, in order to change that you need to comment out the line which reads: bind-address = 127.0.0.1</p>
<p>Change this by editing the following file on your system:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo vi <span style="color: #66cc66;">/</span>etc<span style="color: #66cc66;">/</span>mysql<span style="color: #66cc66;">/</span>my.cnf</pre></td></tr></table></div>

<p>You can search for text in <em>vi</em> using a forward slash.</p>
<p>You should restart MySQL after making a change to the config file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;"><span style="color: #66cc66;">/</span>etc<span style="color: #66cc66;">/</span>init.d<span style="color: #66cc66;">/</span>mysql restart</pre></td></tr></table></div>

<p>Now verify that MySQL is listening to the network. The output of the following command should show an entry for MySQL:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">netstat -tap</pre></td></tr></table></div>

<p>You need to set a password for the root account of MySQL, otherwise anyone can access it!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">mysqladmin -u root password your_password
mysqladmin -h server_name -u root password your_password</pre></td></tr></table></div>

<p>In case it is not clear, <em>your_password</em> should be replaced with a strong password of your choosing.</p>
<h4>Installing Ruby</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">apt-get install ruby ri irb</pre></td></tr></table></div>

<h4>Install Ruby Gems</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">cd <span style="color: #66cc66;">/</span>tmp
wget http:<span style="color: #808080; font-style: italic;">//rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz</span>
tar -zxvf rubygems-0.9.2.tgz
cd <span style="color: #66cc66;">/</span>rubygems-0.9.2.tgz
ruby setup.rb</pre></td></tr></table></div>

<p>Update rubygems</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">gem update --<span style="color: #000000; font-weight: bold;">system</span>
gem update
gem cleanup</pre></td></tr></table></div>

<p>Why the two calls to gem update? &#8211;system will update the RubyGems itself and without that parameter you will only be updating the gems.</p>
<h4>Install Rails</h4>
<p>Install Rails using Rubygems:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">gem install rails</pre></td></tr></table></div>

<p>Now let&#8217;s test the rails installation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">rails <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
cd <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
.<span style="color: #66cc66;">/</span>script<span style="color: #66cc66;">/</span><span style="color: #000066;">server</span></pre></td></tr></table></div>

<p>This will start the default WeBrick web server, so open a web browser to http://localhost:3000 If all is well you will see the Ruby on Rails welcome screen. Once you are finished staring at your success, press CTRL + d to kill the web server.</p>
<h4>Install Mongrel</h4>
<p>Install the build tools:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">apt-get install build-essential ruby1.8-dev</pre></td></tr></table></div>

<p>Install Mongrel:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">gem install mongrel mongrel_cluster --include-dependencies</pre></td></tr></table></div>

<p>Now let&#8217;s test the Mongrel installation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">cd <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
mongrel_rails start</pre></td></tr></table></div>

<p>This will start the Mongrel web server (now default), so open a web browser to http://localhost:3000 If all is well you will see the Ruby on Rails welcome screen&#8230; again. Now kill the web server.</p>
<h4>Configure Mongrel Rails cluster</h4>
<p>Add a user for our mongrel cluster:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">useradd mongrel</pre></td></tr></table></div>

<p>Add the group for our mongrel user:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">groupadd mongrel</pre></td></tr></table></div>

<p>Configure the directory security for your application:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">chown -R mongrel:mongrel <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest</pre></td></tr></table></div>

<p>Now we need to configure the Rails cluster for the test application. This command will write a YAML file to the config directory in your Rails application so you will only have to enter all these parameters once. After that, simply firing the command <em>mongrel_rails cluster::start</em> will use this configuration:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">cd <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
mongrel_rails cluster::configure -e production \
-p <span style="color: #cc66cc;">8000</span> -N <span style="color: #cc66cc;">8</span> --<span style="color: #000066;">user</span> mongrel --group mongrel</pre></td></tr></table></div>

<p>You can reconfigure by running this command and using different parameters, you can also safely delete the YAML file from your config directory to blow these settings away.</p>
<p>Time to start the cluster:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">cd <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
mongrel_rails cluster::start</pre></td></tr></table></div>

<p>You should be able to test your application by going to http://localhost:8000. You could also hit any of the 8 ports you have configured.</p>
<h4>Install Apache</h4>
<p>We have come to the last part of this lengthy installation. Apache. Now, it must be understood that you MUST install Apache 2.2 or higher. The reason for this is the Apache Proxy Balance Module. This module is required to glue all this together and is ONLY available in Apache 2.2 or higher. I am going to build from source on this one because the current version available to me in the apt-get library is not the sufficient version required.</p>
<p>Download Apache, unpack it, and then change to the directory which contains the unpacked files.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">wget http:<span style="color: #808080; font-style: italic;">//archive.apache.org/dist/httpd/httpd-2.2.3.tar.gz</span>
tar xvfz httpd-2.2.3.tar.gz
cd httpd-2.2.3</pre></td></tr></table></div>

<p>Configure the source tree for compiling Apache on your system:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">.<span style="color: #66cc66;">/</span>configure --prefix=<span style="color: #66cc66;">/</span>usr<span style="color: #66cc66;">/</span>local<span style="color: #66cc66;">/</span>apache2 \
--enable-mods-shared=all \
--disable-deflate \
--enable-proxy \
--enable-proxy-balancer \
--enable-proxy-http</pre></td></tr></table></div>

<p>Build and install using the following commands:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">make
sudo make install</pre></td></tr></table></div>

<p>Start Apache and navigate to your server through a browser. You should see the default Apache homepage.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo <span style="color: #66cc66;">/</span>usr<span style="color: #66cc66;">/</span>local<span style="color: #66cc66;">/</span>apache2<span style="color: #66cc66;">/</span>bin<span style="color: #66cc66;">/</span>apachectl start</pre></td></tr></table></div>

<p>There are several options for configuring and securing Apache that should be considered before taking this setup into production.</p>
<p>In order to configure Apache to serve your Mongrel cluster you will need to add the following to the Apache conf file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="io" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>Proxy balancer:<span style="color: #808080; font-style: italic;">//mongrel_cluster&gt;</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8000</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8001</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8002</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8003</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8004</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8005</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8006</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8007</span>
<span style="color: #66cc66;">&lt;/</span>Proxy<span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;</span>VirtualHost yourdomain.com:<span style="color: #cc66cc;">80</span><span style="color: #66cc66;">&gt;</span>
ServerAdmin yourname<span style="color: #66cc66;">@</span>yourdomain.com
ServerName  www.yourdomain.com
ServerAlias yourdomain.com
&nbsp;
<span style="color: #808080; font-style: italic;"># Indexes + Directory Root.</span>
DirectoryIndex test.html
DocumentRoot <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest<span style="color: #66cc66;">/</span>public<span style="color: #66cc66;">/</span>
&nbsp;
<span style="color: #66cc66;">&lt;</span>Directory <span style="color: #ff0000;">&quot;/tmp/railstest/public&quot;</span><span style="color: #66cc66;">&gt;</span>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
<span style="color: #66cc66;">&lt;/</span>Directory<span style="color: #66cc66;">&gt;</span>
&nbsp;
RewriteEngine On
RewriteCond <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#123;</span>HTTP_HOST<span style="color: #66cc66;">&#125;</span> ^yourdomain\.com$ <span style="color: #66cc66;">&#91;</span>NC<span style="color: #66cc66;">&#93;</span>
RewriteRule ^<span style="color: #66cc66;">&#40;</span>.<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span>$ http:<span style="color: #808080; font-style: italic;">//www. yourdomain.com$1 [R=301,L]</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Check for maintenance file and redirect all requests</span>
RewriteCond <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#123;</span>DOCUMENT_ROOT<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">system</span><span style="color: #66cc66;">/</span>maintenance.html -f
RewriteCond <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#123;</span>SCRIPT_FILENAME<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">!</span>maintenance.html
RewriteRule ^.<span style="color: #66cc66;">*</span>$ <span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">system</span><span style="color: #66cc66;">/</span>maintenance.html <span style="color: #66cc66;">&#91;</span>L<span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Rewrite index to check for static</span>
RewriteRule ^<span style="color: #66cc66;">/</span>$ <span style="color: #66cc66;">/</span>index.html <span style="color: #66cc66;">&#91;</span>QSA<span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Rewrite to check for Rails cached page</span>
RewriteRule ^<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>^.<span style="color: #66cc66;">&#93;</span>+<span style="color: #66cc66;">&#41;</span>$ $1.html <span style="color: #66cc66;">&#91;</span>QSA<span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Redirect all non-static requests to cluster</span>
RewriteCond <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#123;</span>DOCUMENT_ROOT<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">/%</span><span style="color: #66cc66;">&#123;</span>REQUEST_FILENAME<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">!</span>-f
RewriteRule ^<span style="color: #66cc66;">/</span><span style="color: #66cc66;">&#40;</span>.<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span>$ balancer:<span style="color: #808080; font-style: italic;">//mongrel_cluster%{REQUEST_URI} [P,QSA,L]</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Logfiles</span>
ErrorLog  <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest<span style="color: #66cc66;">/</span>log<span style="color: #66cc66;">/</span><span style="color: #000066;">error</span>.log
CustomLog <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest<span style="color: #66cc66;">/</span>access.log combined
<span style="color: #66cc66;">&lt;/</span>VirtualHost<span style="color: #66cc66;">&gt;</span>
&nbsp;
NameVirtualHost yourdomain.com:<span style="color: #cc66cc;">80</span></pre></td></tr></table></div>

<p>Now, there are 3 pieces to take note of here. First, you need to create a Proxy group for the mongrel cluster we created. Since we created 8 nodes, there are 8 BalancerMembers. Second, you will need a VirtualHost entry for your Rails site (in this case the railstest site we created earlier). And Finally, you have to add the NameVirtualHost entry for your VirtualHost (I can&#8217;t tell you how many times I forget that).</p>
<p>That should get you going with a Mongrel Cluster behind Apache Proxy. If you want to setup more than one site, then just create 3 more entries in your Apache conf file. You may want to think about separating the declarations from the main httpd.conf file, but for this example I just wanted to get you going. Have Fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/01/18/running-ruby-on-rails-with-mongrel-clustering-and-apache-proxy/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
