<?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>Rails for PHP Developers</title>
	<atom:link href="http://railsforphp.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://railsforphp.com</link>
	<description></description>
	<lastBuildDate>Sat, 19 Dec 2009 05:10:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Contribute to the Reference</title>
		<link>http://railsforphp.com/2009/05/27/contribute-to-the-reference/</link>
		<comments>http://railsforphp.com/2009/05/27/contribute-to-the-reference/#comments</comments>
		<pubDate>Wed, 27 May 2009 18:19:15 +0000</pubDate>
		<dc:creator>Mike Naberezny</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://railsforphp.com/?p=114</guid>
		<description><![CDATA[Many visitors have written in to let us know they found our PHP to Ruby Reference useful and have asked how they can contribute.  We&#8217;re pleased to announce that the source files for the reference are now hosted on GitHub and we&#8217;re accepting contributions.  
If you haven&#8217;t tried Git or GitHub yet, this [...]]]></description>
			<content:encoded><![CDATA[<p>Many visitors have written in to let us know they found our <a href="http://railsforphp.com/reference">PHP to Ruby Reference</a> useful and have asked how they can contribute.  We&#8217;re pleased to announce that the source files for the reference are now <a href="http://github.com/maintainable/php-ruby-reference/">hosted on GitHub</a> and we&#8217;re accepting contributions.  </p>
<p>If you haven&#8217;t tried Git or GitHub yet, this would be a great excuse.  Visit the <a href="http://github.com/guides/home">GitHub Guides</a> for a collection of resources that will help get you started.</p>
<p>Here&#8217;s what you&#8217;ll need to do to contribute:</p>
<ol>
<li>Create a GitHub account for yourself.</li>
<li>Fork <a href="http://github.com/maintainable/php-ruby-reference/">our repository</a> by clicking its <a href="http://github.com/guides/fork-a-project-and-submit-your-modifications">Fork</a> button.</li>
<li>Clone your fork so you can work on it locally.</li>
<li>Make changes, commit them locally, then push your changes to GitHub.</li>
<li>Send us a Pull Request by clicking that button on your GitHub repository.</li>
</ol>
<p>That&#8217;s all there is to it.  Please do not send us pull requests until you&#8217;ve checked the changes or additions you&#8217;ve made and they are ready to be merged.  We&#8217;ll periodically review your contributions and update the website.</p>
<p>Finally, if you haven&#8217;t picked up the <a href="http://pragprog.com/titles/ndphpr/rails-for-php-developers">Rails for PHP Developers book</a> yet, it&#8217;s available in paper and PDF format.  The back of the book includes about 120 pages of additional reference material not found online.</p>
]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2009/05/27/contribute-to-the-reference/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Empty Variables</title>
		<link>http://railsforphp.com/2008/09/23/empty-variables/</link>
		<comments>http://railsforphp.com/2008/09/23/empty-variables/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 23:19:55 +0000</pubDate>
		<dc:creator>Derek DeVries</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://railsforphp.com/?p=58</guid>
		<description><![CDATA[
  div.compare table {
    width: 100%
  }
  div.compare table code {
    background-color: transparent;
  }
  div.compare table .labels {
    color: #009933;
    font-size: 110%
  }
  div.compare span.note {
    color: #b92600;
  }
  div.compare .t [...]]]></description>
			<content:encoded><![CDATA[<style>
  div.compare table {
    width: 100%
  }
  div.compare table code {
    background-color: transparent;
  }
  div.compare table .labels {
    color: #009933;
    font-size: 110%
  }
  div.compare span.note {
    color: #b92600;
  }
  div.compare .t {
    background-color: #d4f0d4; 
    color: #51b351;
  }
  div.compare .f {
    background-color: #f1cccc;
    color: #cf3636;
  }
  div.compare .um {
    background-color: #f0f0f0;
  }
  div.compare td.empty {
    font-size: 30%;
  }
</style>
<p>One of the more confusing differences between Ruby and PHP is the different way in which the languages treat empty variables when evaluating statements. </p>
<p>The chart below shows the most common scenarios you may encounter.  We have several similar charts in the <a href="http://www.pragprog.com/titles/ndphpr/rails-for-php-developers">Rails for PHP Developers</a> book with other handy information.</p>
<div class="compare">
<table cellspacing="0">
<tr>
<th class="labels">PHP</th>
<th>if ($x)</th>
<th>empty($x)</th>
<th>&nbsp;</th>
</tr>
<tr>
<td><code>$x = "";</code></td>
<td class="f">FALSE</td>
<td class="t">TRUE</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><code>$x = null;</code></td>
<td class="f">FALSE</td>
<td class="t">TRUE</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><code>$x = array();</code></td>
<td class="f">FALSE</td>
<td class="t">TRUE</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><code>$x = false;</code></td>
<td class="f">FALSE</td>
<td class="t">TRUE</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><code>$x = 0;</code></td>
<td class="f">FALSE</td>
<td class="t">TRUE</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><code>$x = "0";</code></td>
<td class="f">FALSE</td>
<td class="t">TRUE</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="empty" colspan="4">&nbsp;</td>
</tr>
<tr>
<th class="labels">Ruby</th>
<th>if (x)</th>
<th>x.empty?</th>
<th>x.blank?<span class="note">**</span></th>
</tr>
<tr>
<td><code>x = ""</code></td>
<td class="t">TRUE</td>
<td class="t">TRUE</td>
<td class="t">TRUE</td>
</tr>
<tr>
<td><code>x = nil</code></td>
<td class="f">FALSE</td>
<td class="um">u/m <span class="note">*</span></td>
<td class="t">TRUE</td>
</tr>
<tr>
<td><code>x = []</code></td>
<td class="t">TRUE</td>
<td class="t">TRUE</td>
<td class="t">TRUE</td>
</tr>
<tr>
<td><code>x = {}</code></td>
<td class="t">TRUE</td>
<td class="t">TRUE</td>
<td class="t">TRUE</td>
</tr>
<tr>
<td><code>x = false</code></td>
<td class="f">FALSE</td>
<td class="um">u/m <span class="note">*</span></td>
<td class="t">TRUE</td>
</tr>
<tr>
<td><code>x = 0</code></td>
<td class="t">TRUE</td>
<td class="um">u/m <span class="note">*</span></td>
<td class="f">FALSE</td>
</tr>
<tr>
<td><code>x = "0"</code></td>
<td class="t">TRUE</td>
<td class="f">FALSE</td>
<td class="f">FALSE</td>
</tr>
</table>
</div>
<div class="compare">
<table cellspacing="0">
<tr>
<th><span class="note">*</span> NoMethodError: undefined method</td>
<th><span class="note">**</span> Only Available in Rails</td>
</tr>
</table>
</div>
<p>While PHP evaluates all empty variable as <code>false</code>, Ruby only evaluates <code>nil</code> and <code>false</code> to be <code>false</code>. Some Ruby objects have an <code>empty?</code> method, but this is not always as useful as you might expect. The method is only available on some objects, and calling <code>nil.empty?</code> or <code>false.empty?</code> will result in <code>NoMethodError</code>. </p>
<p>The Rails framework adds a consistent method named <code>blank?</code> that exists on all objects as a single way to check if a variable contains a blank object. Rails has also very recently added a corresponding <code>present?</code> method, which is equivalent of <code>!blank?</code></p>
<h3>Handling Zero</h3>
<p>Handling zero values often trips up beginning Ruby programmers.  This cause of this confusion is best demonstrated by the following two code snippets.  </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// This not evaluated.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">num = <span style="color:#006666;">0</span>
<span style="color:#9966CC; font-weight:bold;">if</span> num
  <span style="color:#008000; font-style:italic;"># This is evaluated!</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

</p>
<p>Refer to the chart above as you read the snippets.  While this may seem counter intuitive coming from PHP, for the most part it&#8217;s not a problem.</p>
<p>In Ruby code, we typically know when we&#8217;re working with numbers, or we can <a href="http://railsforphp.com/2008/09/17/type-casting-and-conversions/">cast to them</a>.  We can then simply do something like these examples:</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">num = User.<span style="color:#9900CC;">count</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> num <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;there are some users&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> num.<span style="color:#9900CC;">zero</span>?
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;no users here&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Similarly, it is important to note that zero (<code>0</code> and <code>"0"</code>) evaluates to <code>true</code> using <code>0.blank?</code>.  </p>
]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2008/09/23/empty-variables/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Type Casting and Conversions</title>
		<link>http://railsforphp.com/2008/09/17/type-casting-and-conversions/</link>
		<comments>http://railsforphp.com/2008/09/17/type-casting-and-conversions/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 18:27:20 +0000</pubDate>
		<dc:creator>Derek DeVries</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://railsforphp.com/?p=43</guid>
		<description><![CDATA[In the Rails for PHP Developers book we mention that Ruby objects have conversion methods to translate between different object types. 
Object Conversion Methods
Methods such as to_s and to_i are very commonly used to translate between different objects. 

  div.compare table code {
    background-color: transparent;
  }
  div.compare table .labels [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.pragprog.com/titles/ndphpr">Rails for PHP Developers</a> book we mention that Ruby objects have conversion methods to translate between different object types. </p>
<h4>Object Conversion Methods</h4>
<p>Methods such as <code>to_s</code> and <code>to_i</code> are very commonly used to translate between different objects. </p>
<style>
  div.compare table code {
    background-color: transparent;
  }
  div.compare table .labels {
    color: #009933;
    font-size: 110%
  }
</style>
<div class="compare">
<table cellspacing="0">
<tr class="labels">
<th></th>
<th>PHP</th>
<th>Ruby</th>
</tr>
<tr>
<th>convert to integer</th>
<td><code>(int) "3";</code></td>
<td><code>"3".to_i</code></td>
</tr>
<tr>
<th>convert to float</th>
<td><code>(float) "1.2";</code></td>
<td><code>"1.2".to_f</code></td>
</tr>
<tr>
<th>convert to string</th>
<td><code>(string) 1;</code></td>
<td><code>1.to_s</code></td>
</tr>
<tr>
<th>convert to array</th>
<td><code>(array) "test";</code></td>
<td><code>[*"test"]</code></td>
</tr>
</table>
</div>
<h4>Kernel Module Conversion Methods</h4>
<p>This isn&#8217;t the only way to convert object types in Ruby. We can use Kernel module conversion methods as well. These methods include <code>Array</code>, <code>Float</code>, <code>Integer</code>, and <code>String</code>. </p>
<div class="compare">
<table cellspacing="0">
<tr class="labels">
<th></th>
<th>PHP</th>
<th>Ruby</th>
</tr>
<tr>
<th>convert to integer</th>
<td><code>(int) "3";</code></td>
<td><code>Integer("3")</code></td>
</tr>
<tr>
<th>convert to float</th>
<td><code>(float) "1.2";</code></td>
<td><code>Float("1.2")</code></td>
</tr>
<tr>
<th>convert to string</th>
<td><code>(string) 1;</code></td>
<td><code>String(1)</code></td>
</tr>
<tr>
<th>convert to array</th>
<td><code>(array) "test";</code></td>
<td><code>Array("test")</code></td>
</tr>
</table>
</div>
<h4>The Differences</h4>
<p>These methods perform similar actions to the instance conversion methods, but have some subtle differences. </p>
<p><code>Integer</code> will honor base indicators such as (0, 0x, and 0b), but <code>String#to_i</code> will not. If Integer cannot convert an object it will throw an <code>ArgumentError</code>. </p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#996600;">&quot;0x12&quot;</span>.<span style="color:#9900CC;">to_i</span>    <span style="color:#008000; font-style:italic;"># =&gt; 0</span>
<span style="color:#CC0066; font-weight:bold;">Integer</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;0x12&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>  <span style="color:#008000; font-style:italic;"># =&gt; 18</span>
&nbsp;
<span style="color:#996600;">&quot;a&quot;</span>.<span style="color:#9900CC;">to_i</span>       <span style="color:#008000; font-style:italic;"># =&gt; 0</span>
<span style="color:#CC0066; font-weight:bold;">Integer</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#008000; font-style:italic;"># =&gt; ArgumentError: invalid value for Integer: &quot;a&quot;</span></pre></div></div>

<p><code>Float</code> will throw a <code>TypeError</code> if you attempt to convert nil to a float. If it cannot convert an object it will throw an <code>ArgumentError</code>.</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0000FF; font-weight:bold;">nil</span>.<span style="color:#9900CC;">to_f</span>    <span style="color:#008000; font-style:italic;"># =&gt; 0.0</span>
<span style="color:#CC0066; font-weight:bold;">Float</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>  <span style="color:#008000; font-style:italic;"># =&gt; TypeError: can't convert nil into Float</span>
&nbsp;
<span style="color:#996600;">&quot;a&quot;</span>.<span style="color:#9900CC;">to_i</span>     <span style="color:#008000; font-style:italic;"># =&gt; 0.0</span>
<span style="color:#CC0066; font-weight:bold;">Float</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#008000; font-style:italic;"># =&gt; ArgumentError: invalid value for Float(): &quot;a&quot;</span></pre></div></div>

<p><code>String</code> works pretty much the same as the <code>to_s</code> method, and is basically just calling the object&#8217;s <code>to_s</code> method. </p>
<p><code>Array</code> will attempt to call <code>object.to_ary</code>, and then <code>object.to_a</code>. If the object doesn&#8217;t respond to either, it will create a single element array with the object. </p>
<h3>Tips</h3>
<p>The great thing about any Ruby errors we might get while attempting to convert types is that they&#8217;re <code>exceptions</code>. This means that we can easily handle them, and set a default value if any conversion doesn&#8217;t work as expected. </p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_var = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;hello&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
my_var.<span style="color:#9900CC;">to_i</span> <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#006666;">0</span>
<span style="color:#008000; font-style:italic;"># =&gt; 0</span>
&nbsp;
my_var = <span style="color:#996600;">&quot;a&quot;</span>
<span style="color:#CC0066; font-weight:bold;">Float</span><span style="color:#006600; font-weight:bold;">&#40;</span>my_var<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#006666;">0.0</span>
<span style="color:#008000; font-style:italic;"># =&gt; 0.0</span></pre></div></div>

<p>In this example, we&#8217;ve used an inline <code>rescue</code> along with a value that we want to assign when an exception is raised during the conversion. </p>
]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2008/09/17/type-casting-and-conversions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Applying Ruby&#8217;s Blocks</title>
		<link>http://railsforphp.com/2008/09/09/applying-rubys-blocks/</link>
		<comments>http://railsforphp.com/2008/09/09/applying-rubys-blocks/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 18:20:56 +0000</pubDate>
		<dc:creator>Mike Naberezny</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://railsforphp.com/?p=19</guid>
		<description><![CDATA[Not long ago, we took a look at the basics of Ruby Block Scope.  When you&#8217;re first getting started with Ruby&#8217;s blocks (closures), little things like that can be frustrating. Blocks can seem so foreign that you might be tempted to think that they&#8217;ll make your code more difficult to read or understand.  [...]]]></description>
			<content:encoded><![CDATA[<p>Not long ago, we took a look at the basics of <a href="http://railsforphp.com/2008/02/18/ruby-block-scope/">Ruby Block Scope</a>.  When you&#8217;re first getting started with Ruby&#8217;s blocks (closures), little things like that can be frustrating. Blocks can seem so foreign that you might be tempted to think that they&#8217;ll make your code more difficult to read or understand.  Once you get past the learning curve, blocks can be leveraged to improve the readability and maintainability of code in some situations.  </p>
<p>Here&#8217;s a few examples of applying Ruby&#8217;s blocks to everyday problems.</p>
<h2>Handling Timeouts</h2>
<p>Ruby&#8217;s <code>Timeout</code> library is used to ensure that a block of code doesn&#8217;t execute longer than a certain amount of time.  This is a handy feature since the block can wrap many operations which might not otherwise support a configurable timeout.</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'timeout'</span>
&nbsp;
Timeout::timeout<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">20</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#008000; font-style:italic;"># Potentially long-running code</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

</p>
<p>The <code>Timeout::timeout</code> method takes a timeout value in seconds.  If the block takes longer to execute than the timeout value, it will be interrupted.</p>
<h2>Working with Files</h2>
<p>In PHP, <a href="http://railsforphp.com/file_get_contents">file_get_contents()</a> and <a href="http://railsforphp.com/file_put_contents">file_put_contents()</a> provide convenient ways to quickly read and write files.  Often, we need to do a bit more.  It&#8217;s common to open a file, perform some operations on it, and then close the file. </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$f</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/foo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$f</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'foo'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</p>
<p>Ruby&#8217;s <code>File.open</code> can be used like above but it can also be passed a block.  </p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/path/to/foobar'</span>, <span style="color:#996600;">'w'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
  f.<span style="color:#9900CC;">write</span> <span style="color:#996600;">'chars'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

</p>
<p>When the block exits, the file will automatically be closed.  This is a best practice for working with files in Rails applications and is noted in the reference page for <a href="/fopen">fopen()</a>.</p>
<p>The block version is nice from a maintenance perspective.  It&#8217;s easy to accidentally remove the call to <a href="http://railsforphp.com/fclose">fclose()</a> and doing so won&#8217;t produce an obvious error.  However, removing the block&#8217;s <code>end</code> will cause a parse error.  In PHP, this is not so much of an issue because PHP will close any file handles left open at the end of each request.</p>
<h2>Performing Benchmarks</h2>
<p>Within the context of the Rails framework, you can call the <code>benchmark</code> method within a controller action.  Given this action:</p>
<p class="rails label">Rails</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#5A0A0A; font-weight:bold;">show</span>
  <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:preferences</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># ...</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

</p>
<p>Just put a block around a piece of code to measure it:</p>
<p class="rails label">Rails</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#5A0A0A; font-weight:bold;">show</span>
  benchmark <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:preferences</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;">&#125;</span>
  <span style="color:#008000; font-style:italic;"># ...</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

</p>
<p>The result of the benchmark will be output to the log, such as <code>log/development.rb</code>.  There is also a <code>benchmark</code> helper method available in all views that does the same.<br />
The Ruby Standard Library also has a Benchmark library you can use outside of Rails.</p>
<h2>Changing the Directory</h2>
<p>If you&#8217;ve ever written a command line script that had to temporarily change the working directory to perform some operations, it might have looked something like this:</p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$here</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getcwd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">chdir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/somewhere/else'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// perform some operations</span>
<span style="color: #990000;">chdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$here</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</p>
<p>You can do the above almost verbatim in Ruby also.  However, a better way to temporarily change the directory is to pass a block to <code>Dir.chdir</code>. </p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">chdir</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/path/to/somewhere/else'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#008000; font-style:italic;"># perform some operations</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

</p>
<p>The working directory will be changed before entering the block, and automatically changed back when the block closes.  Not only is it cleaner, it makes your scripts easier to maintain since you can&#8217;t accidentally remove the returning <code>Dir.chdir</code>.  </p>
]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2008/09/09/applying-rubys-blocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deploying on Phusion Passenger</title>
		<link>http://railsforphp.com/2008/05/13/deploying-on-phusion-passenger/</link>
		<comments>http://railsforphp.com/2008/05/13/deploying-on-phusion-passenger/#comments</comments>
		<pubDate>Wed, 14 May 2008 06:03:03 +0000</pubDate>
		<dc:creator>Mike Naberezny</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://railsforphp.com/?p=23</guid>
		<description><![CDATA[A very large number of PHP developers, perhaps even the majority, are building smaller web applications. These applications receive only a moderate amount of traffic and usually have a single database server, often on the same machine.  
Deployment, or moving your application to production for use by real customers, is largely an afterthought for [...]]]></description>
			<content:encoded><![CDATA[<p>A very large number of PHP developers, perhaps even the majority, are building smaller web applications. These applications receive only a moderate amount of traffic and usually have a single database server, often on the same machine.  </p>
<p>Deployment, or moving your application to production for use by real customers, is largely an afterthought for these small PHP applications.  In many cases, decent PHP code can just be installed on the server and it runs without much trouble.  </p>
<p>One of the points we stress towards the back of <a href="http://www.pragprog.com/titles/ndphpr">our book</a> is that deploying Rails applications can be more difficult than deploying their PHP counterparts.  There are more moving parts and things to you&#8217;ll need to learn.  Until you get the hang of it, deploying even small Rails applications can be frustrating.</p>
<p>A recent development has greatly improved this situation.</p>
<h4>Introducing Phusion Passenger</h4>
<p>PHP can be deployed using a variety of server configurations.  Some of these can be just as frustrating as traditional Rails deployments.  However, the majority of PHP applications are still deployed on <a href="http://httpd.apache.org/">Apache</a> using <code>mod_php</code>.  PHP&#8217;s tight integration with Apache is simple, proven, and just works for many needs.  </p>
<p><a href="http://phusion.nl">Phusion</a> is a small company in the Netherlands that recently released an open source product called <a href="http://modrails.com/">Passenger</a> (<code>mod_rails</code>).  Passenger aims to take the complexity out of deploying Rails applications by also integrating with Apache.  </p>
<p><a href="http://modrails.com/install.html">Installing Passenger</a> on the average Linux or Mac server is usually simple.  It is installed as a gem and contains an automated installer program that compiles and installs the necessary components.  Once installed, many Rails applications can be deployed under Apache simply by configuring a <code>VirtualHost</code> for each application.  The installer even outputs an example for you to copy and paste.  </p>
<p>Passenger gives a deployment experience closer to what we&#8217;ve come to appreciate with PHP.  In the background, there&#8217;s still more moving parts, but Passenger automatically manages them.  It spawns Rails application server processes, proxies to them, and largely eliminates the configuration and glue that other Rails deployment options leave up to you.  Passenger has nice <a href="http://modrails.com/documentation.html">documentation</a> for when some configuration is necessary.</p>
<p>At <a href="http://maintainable.com">Maintainable Software</a>, we develop applications in both PHP and Ruby.  We&#8217;ve been testing Passenger for a few weeks on a dedicated server and recently deployed an application on it.  We have been very impressed with its reliability, performance, and easy-of-use compared to previous Rails deployment options.  You should give Passenger strong consideration, especially if you&#8217;re just starting out.</p>
<h4>Finally, Rails on Shared Hosting</h4>
<p>Traditionally, deploying Rails applications on shared hosts (usually with FastCGI) has been difficult and unreliable.  Due to this, many Rails applications are deployed on small VPS plans from providers like <a href="http://rimuhosting.com/">Rimuhosting</a> and <a href="http://www.slicehost.com/">Slicehost</a>.  These run well, typically using small Mongrel cluster behind a frontend proxy balancer.  However, VPS plans are usually more expensive and more work to set up and maintain than shared hosting.</p>
<p>Passenger makes running Rails applications on shared hosting much better.  <a href="http://dreamhost.com/">Dreamhost</a>, a major shared hosting provider, <a href="http://www.dreamhoststatus.com/2008/05/13/new-feature-passenger-mod_rails/">recently announced</a> full support for Passenger.  We tested a small Rails application on Dreamhost using it and we were impressed.  It seemed to run without issues and with reasonable performance.</p>
<p>Deploying some Rails applications on shared hosting may soon become commonplace.</p>
<h4>Thinking Ahead</h4>
<p>Will better Rails deployment options or Rails on shared hosting mean it&#8217;s time to switch from PHP?  No.  It&#8217;s unlikely this blog will switch from <a href="http://wordpress.org">WordPress</a> anytime soon and Phusion&#8217;s <a href="http://blog.phusion.nl/">own blog</a> even runs on WordPress.  There are many great PHP applications out there.  More importantly, sometimes PHP will simply be a more appropriate solution. </p>
<p>PHP, Ruby, and Rails are all great tools you can choose from.  You&#8217;ll still need to learn these tools, their relative strengths, and when it&#8217;s appropriate to apply each.</p>
<p>Passenger just means Rails deployment is getting simpler and more mature.  It also means deploying PHP and Rails applications together on the same Apache instance is now much easier.  These are more reasons to consider adding Rails to your toolbox.  It&#8217;s certainly a great time to be building web applications.</p>
]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2008/05/13/deploying-on-phusion-passenger/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Separating Public Files</title>
		<link>http://railsforphp.com/2008/05/09/separating-public-files/</link>
		<comments>http://railsforphp.com/2008/05/09/separating-public-files/#comments</comments>
		<pubDate>Fri, 09 May 2008 21:45:00 +0000</pubDate>
		<dc:creator>Derek DeVries</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://railsforphp.com/2008/05/09/separating-public-files/</guid>
		<description><![CDATA[In chapter 5 of the book, we discuss the importance of separating public files in your web applications. Rails applications have a separate public/ directory where all public assets such as CSS, Javascript, and images are stored. No configuration files or Ruby code is stored in this directory, and we don&#8217;t need to worry about [...]]]></description>
			<content:encoded><![CDATA[<p>In chapter 5 of the book, we discuss the importance of separating public files in your web applications. Rails applications have a separate <code>public/</code> directory where all public assets such as CSS, Javascript, and images are stored. No configuration files or Ruby code is stored in this directory, and we don&#8217;t need to worry about server directives being set correctly to secure files our application. </p>
<p>The word <code>public/</code> is perfectly descriptive, and acts as a cognitive warning when serving content. Anything in the <code>public/</code> directory is free and open for users to download. </p>
<p>It&#8217;s a great idea to use this same idea in PHP applications. PHP should interpret files with certain file extensions (such as files ending in .php), but <a href="http://security.phpmagazine.net/2007/08/facebook_code_revealed_mod_php.html">mistakes happen</a>. When you name the web accessible directory of your applications <code>public/</code>, it is immediately obvious as to the accessibility of the content. This raises a red flag when you&#8217;re uploading sensitive documents or  information into a publicly accessible directory. </p>
<p>Most of the PHP frameworks follow this idea, and keep their application logic in a directory that is not publicly accessible from the web. </p>
]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2008/05/09/separating-public-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New PHP to Ruby Reference</title>
		<link>http://railsforphp.com/2008/03/20/new-php-to-ruby-reference/</link>
		<comments>http://railsforphp.com/2008/03/20/new-php-to-ruby-reference/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 17:45:45 +0000</pubDate>
		<dc:creator>Mike Naberezny</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://railsforphp.com/2008/03/20/new-php-to-ruby-reference/</guid>
		<description><![CDATA[If you look up at the navigation bar, you&#8217;ll see we&#8217;ve added a new Reference section.
When you&#8217;re starting out with Ruby and Rails, often times you&#8217;ll know how to do something in PHP and need a way to map that knowledge into the Ruby world.  For example, you might ask yourself, &#8220;What&#8217;s the equivalent [...]]]></description>
			<content:encoded><![CDATA[<p>If you look up at the navigation bar, you&#8217;ll see we&#8217;ve added a new Reference section.</p>
<p>When you&#8217;re starting out with Ruby and Rails, often times you&#8217;ll know how to do something in PHP and need a way to map that knowledge into the Ruby world.  For example, you might ask yourself, &#8220;What&#8217;s the equivalent of <code>fopen</code> in Ruby?&#8221;</p>
<p>Now, you can get the answer just like you would on php.net &#8212; <a href="http://railsforphp.com/fopen">http://railsforphp.com/fopen</a></p>
<p>You can also enter a PHP function name into the search box at the top of every page.  If we have a reference page for that function, you&#8217;ll be taken directly there.</p>
<p>PHP and Ruby are great complementary tools.  You might find that you like Ruby for other tasks like command line scripts.  With this in mind, our reference differentiates between Ruby and Rails.  Solutions that only work in Rails, such as the one for <a href="http://railsforphp.com/reference/url/http_build_query">http_build_query</a>, are clearly marked as such.</p>
<p>To start out, we&#8217;ve seeded the reference with many of the more common functions for working with <a href="http://railsforphp.com/reference/strings">strings</a>, <a href="http://railsforphp.com/reference/pcre">regular expressions</a>, and the <a href="http://railsforphp.com/reference/filesystem">filesystem</a>.  In the coming weeks, we&#8217;ll continue to expand it.</p>
]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2008/03/20/new-php-to-ruby-reference/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Calculating Hashes</title>
		<link>http://railsforphp.com/2008/02/26/calculating-string-and-file-hashes/</link>
		<comments>http://railsforphp.com/2008/02/26/calculating-string-and-file-hashes/#comments</comments>
		<pubDate>Tue, 26 Feb 2008 21:21:27 +0000</pubDate>
		<dc:creator>Derek DeVries</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://railsforphp.com/2008/02/26/calculating-string-and-file-hashes/</guid>
		<description><![CDATA[The MD5 and SHA1 cryptographic hashes are commonly used to verify the integrity of files or create a fixed length digital representation of a string or file. PHP has some very easy functions to create hashes of both strings and files. 
Like most utility functions, generating these hashes is not part of the core Ruby [...]]]></description>
			<content:encoded><![CDATA[<p>The MD5 and SHA1 cryptographic hashes are commonly used to verify the integrity of files or create a fixed length digital representation of a string or file. PHP has some very easy functions to create hashes of both strings and files. </p>
<p>Like most utility functions, generating these hashes is not part of the core Ruby objects.  Instead, the Ruby Standard Library includes the <code>Digest</code> library.  Let&#8217;s take a look at the equivalents to PHP&#8217;s common cryptographic hash functions in the Ruby universe. </p>
<h3>Comparing Functions/Methods</h3>
<div class="compare">
<table cellspacing="0">
<tr>
<th>PHP</th>
<th>Ruby</th>
</tr>
<tr>
<td><a href="#md5">md5</a></td>
<td><a href="#md5">Digest::MD5</a></td>
</tr>
<tr>
<td><a href="#md5_file">md5_file</a></td>
<td><a href="#md5_file">Digest::MD5</a></td>
</tr>
<tr>
<td><a href="#sha1">sha1</a></td>
<td><a href="#sha1">Digest::SHA1<a></td>
</tr>
<tr>
<td><a href="#sha1_file">sha1_file</a></td>
<td><a href="#sha1_file">Digest::SHA1</a></td>
</tr>
<tr>
<td><a href="#crypt">crypt</a></td>
<td><a href="#crypt">String#crypt</a></td>
</tr>
</table>
</div>
<h3 id="md5">md5 vs. Digest::MD5</h3>
<p>In PHP, we use the <code>md5</code> function to calculate an MD5 hash.  In Ruby, we use <code>Digest::MD5</code>.</p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">print</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my string&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// =&gt; 2ba81a47c5512d9e23c435c1f29373cb</span></pre></div></div>

<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;digest&quot;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#6666ff; font-weight:bold;">Digest::MD5</span>.<span style="color:#9900CC;">hexdigest</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;my string&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; 2ba81a47c5512d9e23c435c1f29373cb</span></pre></div></div>

<p>Above, we first required the <code>Digest</code> library using <code>require "digest"</code>. This is similar to using <code>require_once</code> to include a class in PHP. To get the equivalent of the hash we&#8217;d generate using <code>md5()</code>, we use the <code>hexdigest</code> method.</p>
<h3 id="md5_file">md5_file vs. Digest::MD5</h3>
<p>There are generally two different ways in Ruby to generate a cryptographic hash of a file. The first and easiest way is to use <code>File.read</code> to read the entire file contents into memory. We can then use the same <code>MD5.hexdigest</code> method on the contents to generate the hash. </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">print</span> <span style="color: #990000;">md5_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my_file.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// =&gt; 14b6a00b1a4eb40542cfecdc4d64940d</span></pre></div></div>

<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;digest&quot;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#6666ff; font-weight:bold;">Digest::MD5</span>.<span style="color:#9900CC;">hexdigest</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;my_file.jpg&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; 14b6a00b1a4eb40542cfecdc4d64940d</span></pre></div></div>

<p>The code above works for many cases but for larger files, there&#8217;s a risk of consuming large amounts of memory.  For these cases, we can initialize a new digest with <code>Digest::MD5.new</code> and then read the file in smaller chunks, incrementally building the digest with <code>MD5#update</code>.</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;digest&quot;</span>
&nbsp;
digest = <span style="color:#6666ff; font-weight:bold;">Digest::MD5</span>.<span style="color:#9900CC;">new</span>
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;my_file.jpg&quot;</span>, <span style="color:#996600;">&quot;r&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
  digest.<span style="color:#9900CC;">update</span> f.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">8192</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">until</span> f.<span style="color:#9900CC;">eof</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> digest.<span style="color:#9900CC;">hexdigest</span>
<span style="color:#008000; font-style:italic;"># =&gt; 14b6a00b1a4eb40542cfecdc4d64940d</span></pre></div></div>

<h3 id="sha1">sha1 vs. Digest::SHA1</h3>
<p>We use the <code>Digest::SHA1</code> class to build the equivalent cryptographic hash to PHP&#8217;s <code>sha1</code> function. This works the same as the <code>Digest::MD5</code> class mentioned above, and needs to require the <code>Digest</code> library using <code>require "digest"</code>. </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">print</span> <span style="color: #990000;">sha1</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my string&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// =&gt; e19343e6c6c76f8f634a685eba7c0880648b1389</span></pre></div></div>

<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;digest&quot;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#6666ff; font-weight:bold;">Digest::SHA1</span>.<span style="color:#9900CC;">hexdigest</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;my string&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; e19343e6c6c76f8f634a685eba7c0880648b1389</span></pre></div></div>

<h3 id="sha1_file">sha1_file vs. Digest::SHA1</h3>
<p>Just like with <code>MD5</code>, we can generate the SHA1 hash of a file in a couple different ways. We can generate the hash of the entire entire file with <code>File.read</code> </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">print</span> <span style="color: #990000;">sha1_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my_file.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// =&gt; 5de1a26c24b0d176c6cafd11bf1f03017bcc767b</span></pre></div></div>

<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;digest&quot;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#6666ff; font-weight:bold;">Digest::SHA1</span>.<span style="color:#9900CC;">hexdigest</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;my_file.jpg&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; 5de1a26c24b0d176c6cafd11bf1f03017bcc767b</span></pre></div></div>

<p>We can take the same chunked approach we used for <code>MD5</code> to generate a hash for larger files.</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;digest&quot;</span>
&nbsp;
digest = <span style="color:#6666ff; font-weight:bold;">Digest::SHA1</span>.<span style="color:#9900CC;">new</span>
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;my_file.jpg&quot;</span>, <span style="color:#996600;">&quot;r&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
  digest.<span style="color:#9900CC;">update</span> f.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">8192</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">until</span> f.<span style="color:#9900CC;">eof</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> digest.<span style="color:#9900CC;">hexdigest</span>
<span style="color:#008000; font-style:italic;"># =&gt; 5de1a26c24b0d176c6cafd11bf1f03017bcc767b</span></pre></div></div>

<h3 id="crypt">crypt vs. String#crypt</h3>
<p>Finally we have PHP&#8217;s <code>crypt</code> function, which returns an encrypted string using a standard Unix DES-based encryption or an alternate based on the system. This operation has very similar usage in PHP and Ruby. Ruby uses the <code>String#crypt</code> method with a single salt parameter to get the same result. </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">print</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my string&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;salt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// =&gt; saGMxKJ.nmUhU</span></pre></div></div>

<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;my string&quot;</span>.<span style="color:#9900CC;">crypt</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;salt&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; saGMxKJ.nmUhU</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2008/02/26/calculating-string-and-file-hashes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Ruby Block Scope</title>
		<link>http://railsforphp.com/2008/02/18/ruby-block-scope/</link>
		<comments>http://railsforphp.com/2008/02/18/ruby-block-scope/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 17:18:39 +0000</pubDate>
		<dc:creator>Mike Naberezny</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://railsforphp.com/2008/02/18/ruby-block-scope/</guid>
		<description><![CDATA[Ruby&#8217;s blocks, or closures, are a feature that does not have a direct equivalent in PHP.  We devote a fair number of pages to this topic in the book.  Even so, it will take a bit of time and practice before you feel completely comfortable with them.  Let&#8217;s take a look at [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby&#8217;s blocks, or closures, are a feature that does not have a direct equivalent in PHP.  We devote a fair number of pages to this topic in <a href="http://pragprog.com/titles/ndphpr">the book</a>.  Even so, it will take a bit of time and practice before you feel completely comfortable with them.  Let&#8217;s take a look at an easy way that Ruby&#8217;s block scoping might trip you up.</p>
<p>In this example, we have an array of fruit.  We want to iterate through the array and print the name of each fruit.  At the end, we want to print the name of the last fruit again.</p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fruit</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'apple'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'banana'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'orange'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fruit</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$f</span>, &quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">print</span> <span style="color: #000088;">$f</span><span style="color: #339933;">;</span></pre></div></div>

</p>
<p>As you probably expected, here&#8217;s the output of the above program:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">apple, banana, orange, orange</pre></div></div>

<p>This works because PHP has simple scoping rules.  Within a function, any variables that get defined are available any time later in the function.  Variables defined before constructs like <code>foreach()</code> and <code>while()</code> are available inside those constructs.  Variables defined inside those constructs are defined in the same scope and are available outside of those constructs, like <code>$f</code> above.</p>
<p>When you first start writing programs in Ruby, you&#8217;ll probably start out by converting bits of your PHP programs over before you get into the swing.  </p>
<p>With that in mind, let&#8217;s now try directly converting our PHP program to Ruby:</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">fruit = <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#91;</span>apple banana orange<span style="color:#006600; font-weight:bold;">&#93;</span>
fruit.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;#{f}, &quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> f</pre></div></div>

</p>
<p>If you&#8217;re wondering about the <code>%w</code>, that&#8217;s a word array (see <a href="http://railsforphp.com/2008/01/03/useful-perlisms-in-ruby/">Useful Perlisms in Ruby</a> for this and other tricks).  Otherwise, this looks very similar to the PHP version.  </p>
<p>However, you might find the results to be unexpected:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">apple, banana, orange, 
NameError: undefined local variable or method 'f'</pre></div></div>

<p>In Ruby, scoping is lexical.  There can be many levels of scope and scope can even be manipulated.  When a block is called in Ruby, it is bound to the scope of its caller.  This means that within the same method, variables defined <i>above the block</i> are available inside the block.  However, variables defined <i>within the block</i> are not normally available outside the block.  In the example above, Ruby raised a <code>NameError</code> because <code>f</code> was only defined within the block, not above it.</p>
<p>If you really needed to do the example in Ruby, you could define a variable above the block to store the last value through the iteration.</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">fruit = <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#91;</span>apple banana orange<span style="color:#006600; font-weight:bold;">&#93;</span>
last_fruit = <span style="color:#0000FF; font-weight:bold;">nil</span>
&nbsp;
fruit.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> 
  <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;#{f}, &quot;</span> 
  last_fruit = f
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> last_fruit</pre></div></div>

</p>
<p>Since the <code>last_fruit</code> variable is defined above the block, it is available both inside and below the block.  The program now works as you might expect.</p>
<p>While that helps us begin to understand Ruby&#8217;s scoping and gets the job done, a much simpler and more idiomatic Ruby solution for this particular problem would be this:</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">fruit = <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#91;</span>apple banana orange<span style="color:#006600; font-weight:bold;">&#93;</span>
fruit.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;#{f}, &quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> fruit.<span style="color:#9900CC;">last</span></pre></div></div>

</p>
<p>The <a href="http://www.ruby-doc.org/core/classes/Array.html#M002188"><code>Array#last</code></a> method is the equivalent of PHP&#8217;s <a href="http://www.php.net/end"><code>end()</code></a>.  By just using it instead, our code is both more concise and readable.</p>
]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2008/02/18/ruby-block-scope/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Variable Arguments</title>
		<link>http://railsforphp.com/2008/02/13/variable-arguments/</link>
		<comments>http://railsforphp.com/2008/02/13/variable-arguments/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 01:37:23 +0000</pubDate>
		<dc:creator>Derek DeVries</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://railsforphp.com/2008/02/13/variable-arguments/</guid>
		<description><![CDATA[Rails uses quite a few techniques to make the end-user API a little nicer when you&#8217;re creating your applications. One of these techniques is having a method accept variable arguments with a hash of options. We see this pattern appear in quite a few different areas of the Rails API, particularly in ActiveRecord and ActionView. [...]]]></description>
			<content:encoded><![CDATA[<p>Rails uses quite a few techniques to make the end-user API a little nicer when you&#8217;re creating your applications. One of these techniques is having a method accept variable arguments with a hash of options. We see this pattern appear in quite a few different areas of the Rails API, particularly in ActiveRecord and ActionView. </p>
<p>An an example, we can specify a variable number of attributes when we perform model validation. Take a look at how we validate the presence of both the <code>name</code> and <code>age</code> attributes for a model along with a custom error message as the final argument. </p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:age</span>, <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Can't be empty!&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>We also see this pattern used in various built-in view helper methods. When we want to include multiple stylesheets in our application, we can specify multiple sources using <code>stylesheet_link_tag</code>. </p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&lt;head&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= stylesheet_link_tag <span style="color:#ff3333; font-weight:bold;">:layout</span>, <span style="color:#ff3333; font-weight:bold;">:colors</span>, <span style="color:#ff3333; font-weight:bold;">:media</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;screen&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&lt;/head&gt;</pre></div></div>

<p>Using variable arguments is often more convenient than calling a function multiple times, and it is a little nicer than passing an array. This trick can be adapted to PHP development pretty easily. The above example looks quite different from PHP when using the common Ruby style. </p>
<p>However, it does map faithfully to PHP if we add back the syntactic noise that we purposely left out.  We also talk about this transliteration in the Beginning Ruby Code chapter of <a href="http://pragprog.com/titles/ndphpr">the book</a>.</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= stylesheet_link_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:layout</span>, <span style="color:#ff3333; font-weight:bold;">:color</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:media <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;screen&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?=</span> stylesheetLinkTag<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'layout'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'color'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'media'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'screen'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>In PHP we implement variable arguments using using the <code>func_get_args()</code> function:</p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> stylesheetLinkTag<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sources</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sources</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sources</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$source</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// ...</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Since the <code>$sources</code> come from <code>func_get_args()</code>, no arguments in the function definition are necessary.  However, if the function definition has no arguments, PHP will not raise a notice if the function is called with no arguments.  Since our function requires at least one argument, it&#8217;s useful to have it in the function definition for that reason.  It will also get picked up by code documentation tools.</p>
<p>Let&#8217;s not forget about the optional list of options that we pass in as the final argument. We can extract this data in our PHP function implementation by simply checking if the last value of the variable arguments is an array. </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> stylesheetLinkTag<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sources</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$sources</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$last</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sources</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$last</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sources</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// find the stylesheet media attribute</span>
  <span style="color: #000088;">$media</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'media'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'media'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sources</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$source</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2008/02/13/variable-arguments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
