<?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"
	>

<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>
	<pubDate>Fri, 09 May 2008 21:45:00 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<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/?p=24</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>
		</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>
		</item>
		<item>
		<title>Calculating Hashes of Strings and Files</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"><span style="color: #000066;">print</span> <span style="color: #000066;">md5</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;my string&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; 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"><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"><span style="color: #000066;">print</span> <span style="color: #000066;">md5_file</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;my_file.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; 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"><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"><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> |f|
  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"><span style="color: #000066;">print</span> <span style="color: #000066;">sha1</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;my string&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; 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"><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"><span style="color: #000066;">print</span> <span style="color: #000066;">sha1_file</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;my_file.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; 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"><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"><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> |f|
  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"><span style="color: #000066;">print</span> <span style="color: #000066;">crypt</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;my string&quot;</span>, <span style="color: #ff0000;">&quot;salt&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; 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"><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>
		</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"><span style="color: #0000ff;">$fruit</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'apple'</span>, <span style="color: #ff0000;">'banana'</span>, <span style="color: #ff0000;">'orange'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fruit</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$f</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;$f, &quot;</span>; <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$f</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>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">fruit = %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> |f| <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>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">fruit = %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> |f| 
  <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">fruit = %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> |f| <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>
		</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"><span style="color:#9966CC; font-weight:bold;">class</span> User &lt; <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> =&gt; <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">&lt;head&gt;
  &lt;%= 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> =&gt; <span style="color:#996600;">&quot;screen&quot;</span> %&gt;
&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">&lt;%= 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 =&gt; <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> %&gt;</pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?</span>= stylesheetLinkTag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'layout'</span>, <span style="color: #ff0000;">'color'</span>, <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'media'</span> =&gt; <span style="color: #ff0000;">'screen'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#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"><span style="color: #000000; font-weight: bold;">function</span> stylesheetLinkTag<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sources</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$sources</span> = <span style="color: #000066;">func_get_args</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sources</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$source</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">// ...</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#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"><span style="color: #000000; font-weight: bold;">function</span> stylesheetLinkTag<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sources</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0000ff;">$sources</span> = <span style="color: #000066;">func_get_args</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #0000ff;">$last</span>    = <span style="color: #000066;">end</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sources</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #0000ff;">$options</span> = <span style="color: #000066;">is_array</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$last</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #000066;">array_pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sources</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">// find the stylesheet media attribute</span>
  <span style="color: #0000ff;">$media</span> = <span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'media'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'media'</span><span style="color: #66cc66;">&#93;</span> : <span style="color: #ff0000;">''</span>;
&nbsp;
  <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sources</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$source</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// ...</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2008/02/13/variable-arguments/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Regular Expressions in Ruby</title>
		<link>http://railsforphp.com/2008/01/17/regular-expressions-in-ruby/</link>
		<comments>http://railsforphp.com/2008/01/17/regular-expressions-in-ruby/#comments</comments>
		<pubDate>Fri, 18 Jan 2008 01:38:25 +0000</pubDate>
		<dc:creator>Derek DeVries</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://railsforphp.com/2008/01/17/regular-expressions-in-ruby/</guid>
		<description><![CDATA[PHP implements both POSIX and Perl-compatible regular expressions. The Perl-compatible regexp functions (which includes all the preg_* functions) is the preferred library for most developers since it has many features not available in POSIX, and is binary safe. 
Ruby uses Perl-compatible regular expressions, so if you&#8217;re familiar with the preg_* functions in PHP, you&#8217;re already [...]]]></description>
			<content:encoded><![CDATA[<p>PHP implements both POSIX and Perl-compatible regular expressions. The Perl-compatible regexp functions (which includes all the <code>preg_*</code> functions) is the preferred library for most developers since it has many features not available in POSIX, and is binary safe. </p>
<p>Ruby uses Perl-compatible regular expressions, so if you&#8217;re familiar with the <code>preg_*</code> functions in PHP, you&#8217;re already well on your way to learning regular expressions in Ruby. Regular expressions are a complex topic, so we won&#8217;t be covering regular expression basics, but will instead focus on translating existing knowledge of Perl-compatible PHP functions to Ruby.</p>
<h3>Regular Expressions in Ruby</h3>
<p>We use regular expression patterns in PHP by passing a string argument to various functions. Ruby treats regular expressions differently. Instead of specifying the pattern within a string, they are objects just like everything else in Ruby. </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #0000ff;">$myRegexp</span> = <span style="color: #ff0000;">'/[a-z0-9]+<span style="color: #000099; font-weight: bold;">\s</span>/mi'</span>;
<span style="color: #000066;">print</span> <span style="color: #000066;">gettype</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$myRegexp</span><span style="color: #66cc66;">&#41;</span>; 
<span style="color: #808080; font-style: italic;">// =&gt; string</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="ruby">my_regexp = /<span style="color:#006600; font-weight:bold;">&#91;</span>a-z0<span style="color:#006666;">-9</span><span style="color:#006600; font-weight:bold;">&#93;</span>+\s/mi
<span style="color:#CC0066; font-weight:bold;">p</span> my_regexp.<span style="color:#9966CC; font-weight:bold;">class</span>
<span style="color:#008000; font-style:italic;"># =&gt; Regexp</span></pre></div></div>

<p>We can create regular expressions in Ruby using two different literal syntaxes.<br />
The most common is by enclosing the pattern in forward-slashes, but we can also use an alternate <code>%r{}</code> syntax. We usually use <code>%r{}</code> when the pattern contains a lot of forward-slashes (such as a filepath). Regular expressions can also be explicitly instantiated using the Regexp class.</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">/<span style="color:#006600; font-weight:bold;">&#91;</span>a-z0<span style="color:#006666;">-9</span><span style="color:#006600; font-weight:bold;">&#93;</span>+\s/mi
%r<span style="color:#006600; font-weight:bold;">&#123;</span>/path/to/gif\.<span style="color:#9900CC;">gif</span><span style="color:#006600; font-weight:bold;">&#125;</span>mi
<span style="color:#CC00FF; font-weight:bold;">Regexp</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[a-z0-9]+<span style="color:#000099;">\s</span>&quot;</span>, <span style="color:#6666ff; font-weight:bold;">Regexp::IGNORECASE</span> | <span style="color:#6666ff; font-weight:bold;">Regexp::MULTILINE</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<h3>Comparing Functions/Methods</h3>
<p>Now that we&#8217;ve seen some basic syntax for regular expression objects in Ruby, let&#8217;s take a look at PHP&#8217;s PCRE functions, and their closest equivalents in Ruby. </p>
<div class="compare">
<table cellspacing="0">
<tr>
<th>PHP</th>
<th>Ruby</th>
</tr>
<tr>
<td><a href="#preg_match">preg_match</a></td>
<td><a href="#preg_match">String#match</a></td>
</tr>
<tr>
<td><a href="#preg_match_all">preg_match_all</a></td>
<td><a href="#preg_match_all">String#scan</a></td>
</tr>
<tr>
<td><a href="#preg_replace">preg_replace</a></td>
<td><a href="#preg_replace">String#gsub</a></td>
</tr>
<tr>
<td><a href="#preg_split">preg_split</a></td>
<td><a href="#preg_split">String#split</a></td>
</tr>
<tr>
<td><a href="#preg_grep">preg_grep</a></td>
<td><a href="#preg_grep">Array#grep</a></td>
</tr>
<tr>
<td><a href="#preg_quote">preg_quote</a></td>
<td><a href="#preg_quote">Regexp.escape</a></td>
</tr>
</table>
</div>
<h3 id="preg_match">preg_match vs. String#match</h3>
<p>We match a pattern in Ruby strings using the <code>match</code> method. Ruby&#8217;s <code>match</code> method works differently than <code>preg_match</code> in how it returns matches. We usually want to know two different things when we match data: If the pattern matched, and what specific strings sections were matched. </p>
<p>PHP returns an integer to tell us if the data matched (either <code>0</code> or <code>1</code>) and populates a matches array by reference. Ruby returns a <code>MatchData</code> object when the pattern matches, and <code>nil</code> when something doesn&#8217;t. We can inspect the <code>MatchData</code> object to find the actual string matches. </p>
<p>In this example, we try to match the different components of a list of email addresses. Both <code>preg_match</code> and <code>String#match</code> only match the first occurrence of the pattern. </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #0000ff;">$string</span> = <span style="color: #ff0000;">'joe@example.com; walter@example.org'</span>;
<span style="color: #0000ff;">$result</span> = <span style="color: #000066;">preg_match</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/([a-z0-9_.-]+)@([a-z0-9-]+)<span style="color: #000099; font-weight: bold;">\.</span>([a-z.]+)/i'</span>, 
          <span style="color: #0000ff;">$string</span>, <span style="color: #0000ff;">$matches</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">var_export</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>; 
<span style="color: #808080; font-style: italic;">// =&gt; 1</span>
&nbsp;
<span style="color: #000066;">var_export</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$matches</span><span style="color: #66cc66;">&#41;</span>; 
<span style="color: #808080; font-style: italic;">// =&gt; array('joe@example.com', 'joe', 'example', 'com')</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">'joe@example.com; walter@example.org'</span>
matches = <span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>/<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>a-z0-9_.-<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;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>a-z0<span style="color:#006666;">-9</span>-<span style="color:#006600; font-weight:bold;">&#93;</span>+<span style="color:#006600; font-weight:bold;">&#41;</span>\.<span style="color:#9900CC;"><span style="color:#006600; font-weight:bold;">&#40;</span></span><span style="color:#006600; font-weight:bold;">&#91;</span>a-z.<span style="color:#006600; font-weight:bold;">&#93;</span>+<span style="color:#006600; font-weight:bold;">&#41;</span>/i<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> !matches.<span style="color:#0000FF; font-weight:bold;">nil</span>?
<span style="color:#008000; font-style:italic;"># =&gt; true</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> matches
<span style="color:#008000; font-style:italic;"># =&gt; #&lt;MatchData:0x1ed138&gt;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> matches<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#008000; font-style:italic;"># =&gt; &quot;joe&quot;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> matches.<span style="color:#9900CC;">to_a</span>
<span style="color:#008000; font-style:italic;"># [&quot;joe@example.com&quot;, &quot;joe&quot;, &quot;example&quot;, &quot;com&quot;]</span></pre></div></div>

<h3 id="preg_match_all">preg_match_all vs. String#scan</h3>
<p>PHP returns an integer with the number of matches for <code>preg_match_all</code> and populates a matches array by reference. Ruby performs multiple matches for a string using the <code>scan</code> method. This method returns a nested array of matches or an empty array when no matches are found. Be aware that the nesting of values in this array is different than how <code>preg_match_all</code> orders matches. </p>
<p>In this example, we match components of the email address, and both <code>preg_match_all</code> and <code>String#scan</code> give us an array of matches that are found. </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #0000ff;">$string</span> = <span style="color: #ff0000;">'joe@example.com; walter@example.org'</span>;
<span style="color: #0000ff;">$result</span> = <span style="color: #000066;">preg_match_all</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/([a-z0-9_.-]+)@([a-z0-9-]+)<span style="color: #000099; font-weight: bold;">\.</span>[a-z.]+/i'</span>, 
          <span style="color: #0000ff;">$string</span>, <span style="color: #0000ff;">$matches</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">var_export</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// =&gt; 2</span>
&nbsp;
<span style="color: #000066;">var_export</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$matches</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// =&gt; array(array('joe@example.com', 'walter@example.org'),</span>
<span style="color: #808080; font-style: italic;">//          array('joe',             'walter'),</span>
<span style="color: #808080; font-style: italic;">//          array('example',         'example'),</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">'joe@example.com; walter@example.org'</span>
result = <span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span>/<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>a-z0-9_.-<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;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>a-z0<span style="color:#006666;">-9</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;">&#91;</span>a-z.<span style="color:#006600; font-weight:bold;">&#93;</span>+/i<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> result.<span style="color:#9900CC;">size</span>
<span style="color:#008000; font-style:italic;"># =&gt; 2</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> result
<span style="color:#008000; font-style:italic;"># =&gt; [[&quot;joe&quot;, &quot;example&quot;], [&quot;walter&quot;, &quot;example&quot;]]</span></pre></div></div>

<h3 id="preg_replace">preg_replace vs. String#gsub</h3>
<p>We perform pattern based substitution in Ruby using <code>gsub</code>, which is equivalent to PHP&#8217;s <code>preg_replace</code> function. A notable difference is that the <code>gsub</code> method is also used for string substitution in Ruby. We do this by simply providing a string instead of the regular expression pattern. This would be like using the <code>str_replace</code> function in PHP.</p>
<p>In this example, we want to replace the domain in all emails with <code>foo</code>. </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #0000ff;">$string</span> = <span style="color: #ff0000;">'joe@example.com; walter@example.org'</span>;
<span style="color: #0000ff;">$result</span> = <span style="color: #000066;">preg_replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/@([a-z0-9-]+)/'</span>, <span style="color: #ff0000;">'@foo'</span>, <span style="color: #0000ff;">$string</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">var_export</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// =&gt; 'joe@foo.com; walter@foo.org'</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">'joe@example.com; walter@example.org'</span>
result = <span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/@<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>a-z0<span style="color:#006666;">-9</span>-<span style="color:#006600; font-weight:bold;">&#93;</span>+<span style="color:#006600; font-weight:bold;">&#41;</span>/, <span style="color:#996600;">'@foo'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> result
<span style="color:#008000; font-style:italic;"># =&gt; &quot;joe@foo.com; walter@foo.org&quot;</span></pre></div></div>

<p>We can use backreferences in our <code>gsub</code> replacements just as we would with <code>preg_replace</code> by using <code>\1</code>, <code>\2</code>, etc in our replacement string. </p>
<p>In this example, we prefix the existing domain with <code>mail.</code>. Remember to escape backslashes used for the backreference.</p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #808080; font-style: italic;">// Replace domain with mail.domain</span>
<span style="color: #0000ff;">$string</span> = <span style="color: #ff0000;">'joe@example.com; walter@example.org'</span>;
<span style="color: #0000ff;">$result</span> = <span style="color: #000066;">preg_replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/@([a-z0-9-]+)/'</span>, <span style="color: #ff0000;">'@mail.<span style="color: #000099; font-weight: bold;">\\</span>1'</span>, <span style="color: #0000ff;">$string</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">var_export</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// =&gt; 'joe@mail.example.com; walter@mail.example.org'</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">'joe@example.com; walter@example.org'</span>
result = <span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/@<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>a-z0<span style="color:#006666;">-9</span>-<span style="color:#006600; font-weight:bold;">&#93;</span>+<span style="color:#006600; font-weight:bold;">&#41;</span>/, <span style="color:#996600;">'@mail.<span style="color:#000099;">\\</span>1'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> result
<span style="color:#008000; font-style:italic;"># =&gt; &quot;joe@mail.example.com; walter@mail.example.org&quot;</span></pre></div></div>

<h3 id="preg_split">preg_split vs. String#split</h3>
<p>We split strings by a pattern in Ruby using the <code>split</code> method. This is pretty much the same as the <code>preg_split</code> function in PHP. As with gsub, we can also use this same method to split using a string instead of a regular expression. This means that <code>split</code> also performs the equivalent of the <code>explode</code> function in PHP. </p>
<p>In this example, we create an array of the list of emails by splitting the string using the semi-colon and space as the delimiter. </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php">  <span style="color: #0000ff;">$string</span> = <span style="color: #ff0000;">'joe@example.com; walter@example.org'</span>;
  <span style="color: #0000ff;">$result</span> = <span style="color: #000066;">preg_split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/;<span style="color: #000099; font-weight: bold;">\s</span>?/'</span>, <span style="color: #0000ff;">$string</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #000066;">var_export</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #808080; font-style: italic;">// array('joe@example.com', 'walter@example.org')</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">'joe@example.com; walter@example.org'</span>
  result = <span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span>/;\s?/<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">p</span> result
  <span style="color:#008000; font-style:italic;"># =&gt; [&quot;joe@example.com&quot;, &quot;walter@example.org&quot;]</span></pre></div></div>

<h3 id="preg_grep">preg_grep vs. Array#grep</h3>
<p>The <code>preg_grep</code> function in PHP is a useful function to find entries in an array that match a given pattern. Ruby does this same operation with the <code>grep</code> method. </p>
<p>In this example, we&#8217;ll build a new array that only consists of email addresses that end in <code>.com</code>.</p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #0000ff;">$myArray</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'joe@example.com'</span>, <span style="color: #ff0000;">'walter@example.org'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$result</span> = <span style="color: #000066;">preg_grep</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/<span style="color: #000099; font-weight: bold;">\.</span>com$/'</span>, <span style="color: #0000ff;">$myArray</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">var_export</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// =&gt; array('joe@example.com')</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="ruby">my_array = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'joe@example.com'</span>, <span style="color:#996600;">'walter@example.org'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
result = my_array.<span style="color:#9900CC;">grep</span><span style="color:#006600; font-weight:bold;">&#40;</span>/\.<span style="color:#9900CC;">com</span>$/<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> result
<span style="color:#008000; font-style:italic;"># =&gt; [&quot;joe@example.com&quot;]</span></pre></div></div>

<h3 id="preg_quote">preg_quote vs. Regexp.quote</h3>
<p>When we use a string as a regular expression, we want to escape the characters that could be interpreted as regexp special characters. PHP does this using <code>preg_quote</code>, and Ruby has an equivalent <code>Regexp.escape</code> method. </p>
<p>In this example, we&#8217;ll escape any regular expression special character in the given string.  </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #0000ff;">$string</span> = <span style="color: #ff0000;">'[my_file.gif]'</span>;
<span style="color: #0000ff;">$result</span> = <span style="color: #000066;">preg_quote</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$string</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">var_export</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// =&gt; '\\[my_file\\.gif\\]'</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">'[my_file.gif]'</span>
result = <span style="color:#CC00FF; font-weight:bold;">Regexp</span>.<span style="color:#9900CC;">escape</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">string</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> result
<span style="color:#008000; font-style:italic;"># =&gt; &quot;\\[my_file\\.gif\\]&quot;</span></pre></div></div>

<h3>Regular Expressions in Rails</h3>
<p>Rails uses regular expressions in various places to specify patterns. When we are matching a route in Rails, we can use them to assign a requirement that a route component must match:</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#6666ff; font-weight:bold;">ActionController::Routing::Routes</span>.<span style="color:#9900CC;">draw</span> <span style="color:#9966CC; font-weight:bold;">do</span> |map|
  map.<span style="color:#9900CC;">connect</span> <span style="color:#996600;">'teams/:team_id/players/:action/:id, :team_id =&gt; /<span style="color:#000099;">\d</span>+/
end</span></pre></div></div>

<p>We can also use regular expressions in our models when we validate the format of data. We pass a regexp to the <code>:with</code> option of <code>validates_format_of</code>:</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Image &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  validates_format_of <span style="color:#ff3333; font-weight:bold;">:url</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> =&gt; /\.<span style="color:#9900CC;"><span style="color:#006600; font-weight:bold;">&#40;</span>gif</span>|jpg<span style="color:#006600; font-weight:bold;">&#41;</span>/i, 
                      <span style="color:#ff3333; font-weight:bold;">:message</span> =&gt; <span style="color:#996600;">&quot;must be a GIF or JPG&quot;</span> 
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>When we are testing controller code, the <code>assert_select</code> method will accept a regular expression to match response data according to the given pattern. </p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> HomepageControllerTest &lt; <span style="color:#6666ff; font-weight:bold;">ActionController::TestCase</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> test_greeting
    get <span style="color:#ff3333; font-weight:bold;">:index</span>
    assert_select <span style="color:#996600;">'div.greeting'</span>, /Welcome <span style="color:#006600; font-weight:bold;">&#91;</span>a-z0<span style="color:#006666;">-9</span>-_<span style="color:#006600; font-weight:bold;">&#93;</span>+/
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2008/01/17/regular-expressions-in-ruby/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP, Meet YAML</title>
		<link>http://railsforphp.com/2008/01/08/php-meet-yaml/</link>
		<comments>http://railsforphp.com/2008/01/08/php-meet-yaml/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 23:32:49 +0000</pubDate>
		<dc:creator>Mike Naberezny</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://railsforphp.com/2008/01/08/php-meet-yaml/</guid>
		<description><![CDATA[Just about every PHP application needs some kind of configuration, if only to define the connection to a database.  One popular way to store configuration is to use a PHP file itself because it&#8217;s convenient and fast for PHP to read.  This approach is taken by the Solar PHP Framework and many others.
However, [...]]]></description>
			<content:encoded><![CDATA[<p>Just about every PHP application needs some kind of configuration, if only to define the connection to a database.  One popular way to store configuration is to use a PHP file itself because it&#8217;s convenient and fast for PHP to read.  This approach is taken by the <a href="http://solarphp.com/manual/Solar/Config_file">Solar PHP Framework</a> and many others.</p>
<p>However, while PHP is convenient for us as developers, it&#8217;s often inconvenient for others.  For example, we often leave the task of deploying and configuring our applications with system administrators.  For those sysadmins without PHP knowledge, it&#8217;s easy to become frustrated by a missing quote, parenthesis, or semicolon in a PHP file.  </p>
<h3>Introducing YAML</h3>
<p>Besides PHP itself, the most popular config file formats for PHP applications are INI and XML files.  More people understand these formats, but these have problems of their own.  The INI format is easy-to-use but not great at representing hierarchical data.  XML improves on hierarchical data but is not nearly as easy-to-use as INI.</p>
<p><a href="http://yaml.org/">YAML</a> is a relatively new format that has been pioneered by the Ruby and Rails communities.  It blends the best aspects of XML and INI, giving us a format with the flexibility of XML and the ease-of-use of INI.  </p>
<p>Here&#8217;s what a snippet of YAML looks like:</p>

<div class="wp_syntax"><div class="code"><pre>development:
  adapter:  mysql
  socket:   /tmp/mysql.sock
  encoding: utf8
  database: newsletter_development
  username: the-username
  password: the-password</pre></div></div>

<p>The YAML site has a <a href="http://yaml.org/start.html">more complex</a> example showing what&#8217;s possible.</p>
<p>All Rails applications use the same <code>config/database.yml</code> file to configure the database connection.  The snippet above was taken from Chapter 1 in <a href="http://pragprog.com/titles/ndphpr">the book</a>, where we developed a simple newsletter application using Rails.  </p>
<p>Let&#8217;s explore Ruby&#8217;s built-in YAML support and then look at it from a PHP perspective.</p>
<h3>Ruby&#8217;s YAML Support</h3>
<p>One of the strengths of Rails is that it is built on a foundation of solid Ruby tools.  Rails got the ability to read YAML files for free because YAML support is bundled with Ruby.  Fire up IRB and try it yourself:</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">irb&gt; <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;yaml&quot;</span>
=&gt; <span style="color:#0000FF; font-weight:bold;">true</span>
irb&gt; h = <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#CC0066; font-weight:bold;">load</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo: bar<span style="color:#000099;">\n</span>baz: qux&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
=&gt; <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;foo&quot;</span>=&gt;<span style="color:#996600;">&quot;bar&quot;</span>, <span style="color:#996600;">&quot;baz&quot;</span>=&gt;<span style="color:#996600;">&quot;qux&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
irb&gt; h<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;foo&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
=&gt; <span style="color:#996600;">&quot;bar&quot;</span></pre></div></div>

</p>
<p>Above, we put a YAML document into a string.  The <code>YAML.load</code> method then parses it into a Ruby hash, which is simple to use.  </p>
<p>If you have a Rails application handy, such as the newsletter or user group apps from the book, change to the application&#8217;s base directory and try this from IRB:</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">irb&gt; <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;yaml&quot;</span>
=&gt; <span style="color:#0000FF; font-weight:bold;">true</span>
irb&gt; h = <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#9900CC;">load_file</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;config/database.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
=&gt; <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;development&quot;</span>=&gt;<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;adapter&quot;</span>=&gt;<span style="color:#996600;">&quot;mysql&quot;</span>, <span style="color:#996600;">&quot;encoding&quot;</span>=&gt;<span style="color:#996600;">&quot;utf8&quot;</span> ... <span style="color:#006600; font-weight:bold;">&#125;</span>
irb&gt; h<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'development'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'database'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
=&gt; <span style="color:#996600;">&quot;newsletter_development&quot;</span></pre></div></div>

</p>
<p>You can see it&#8217;s relatively painless for Rails to read the <code>config/database.yml</code> file just with the YAML support included with Ruby.</p>
<h3>PHP and YAML</h3>
<p>PHP 5 includes built-in support for reading many different formats including INI, XML, and JSON.  Unfortunately, YAML support is not yet bundled with PHP like it is with Ruby.</p>
<p>A few different solutions have appeared to fill this gap.  One is <a href="http://spyc.sourceforge.net/">Spyc<a/>, a pure-PHP library originally for PHP 4 that now includes a PHP 5 version.  Another is <a href="http://pecl.php.net/package/syck">pecl/syck</a>, a fast PHP extension that provides bindings to the Syck library written in C.</p>
<p>Perhaps the best at this time is <a href="http://pear.horde.org/index.php?package=yaml">Horde/Yaml</a>.  This library is PHP 5 E_STRICT complaint, uses the familiar PEAR coding standards, and is based on Spyc.  Horde/Yaml includes a number of bug fixes, cleanup, and will transparently use pecl/syck if it is available.  </p>
<p>Now, we&#8217;ll do the same exercises from above but with PHP and Horde/Yaml.</p>
<h3>Installing Horde/Yaml</h3>
<p>The Horde/Yaml library is distributed as a <a href="http://pear.php.net">PEAR</a> package, the analog of <a href="http://http://rubygems.org/">RubyGems</a>.  </p>
<p>To install it, run these commands from your shell.  If you&#8217;re on a Unix-like machine, you will probably have to add <code>sudo</code> before these commands.</p>

<div class="wp_syntax"><div class="code"><pre>shell&gt; pear channel-discover pear.horde.org
shell&gt; pear install horde/yaml</pre></div></div>

<p>If installation is successful, you should see <code>install ok</code>.  </p>
<h3>Using Horde/Yaml</h3>
<p>Horde/Yaml, like many newer PHP 5 libraries, does not explicitly use <code>require</code> to load its files.  Instead, it uses <a href="http://www.php.net/autoload">autoloading</a>.  If you&#8217;re unfamiliar with this or don&#8217;t yet have an autoloader set up, adding something like this to the top of your script will get it working:</p>
<p class="php label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">function</span> sample_autoloader<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$class</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">require</span> <span style="color: #000066;">str_replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'_'</span>, <span style="color: #ff0000;">'/'</span>, <span style="color: #0000ff;">$class</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">'.php'</span>;
<span style="color: #66cc66;">&#125;</span>
spl_autoload_register<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'sample_autoloader'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

</p>
<p>With that out of the way, Horde/Yaml is ready to go.</p>
<p>Let&#8217;s look at the equivalent of the first Ruby example, where a string containing YAML is loaded into a PHP associative array:</p>
<p class="php label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #0000ff;">$a</span> = Horde_Yaml::<span style="color: #006600;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;foo: bar<span style="color: #000099; font-weight: bold;">\n</span>baz: qux&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">var_export</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

</p>
<p>The <code>Horde_Yaml::load()</code> method parses YAML into a variable, just like its Ruby counterpart.  The output of <code>var_export()</code> is:</p>

<div class="wp_syntax"><div class="code"><pre>array (
  'foo' =&gt; 'bar',
  'baz' =&gt; 'qux',
)</pre></div></div>

<p>Horde/Yaml can also read from a file with <code>loadFile()</code> or an open stream with <code>loadStream()</code>.  </p>
<p>That&#8217;s all you need to start reading YAML in PHP.</p>
<h3>Notes</h3>
<p>Be careful in your PHP applications that sensitive YAML files are not web-accessible. Keep them outside of the document root or use an <code>.htaccess</code> file if that&#8217;s not an option.  If you don&#8217;t, your web server will give them to anyone who requests.</p>
<p>This article focuses on reading YAML, but both Horde/Yaml and Ruby&#8217;s built-in YAML support can dump data structures into YAML.  Just pass a hash (associative array) to the <code>dump()</code> method of either to get the equivalent YAML string.</p>
]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2008/01/08/php-meet-yaml/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Useful Perlisms in Ruby</title>
		<link>http://railsforphp.com/2008/01/03/useful-perlisms-in-ruby/</link>
		<comments>http://railsforphp.com/2008/01/03/useful-perlisms-in-ruby/#comments</comments>
		<pubDate>Thu, 03 Jan 2008 16:08:48 +0000</pubDate>
		<dc:creator>Mike Naberezny</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://railsforphp.com/2008/01/03/useful-perlisms-in-ruby/</guid>
		<description><![CDATA[PHP syntax shows obvious similarities to Perl.  For example, we have the prefixing of variables ($), the arrow for object access (-&#62;), and semicolons terminating our statements.  While it&#8217;s perhaps less obvious to PHP developers at first, Ruby&#8217;s syntax has also been influenced by Perl in various ways.  
Ruby has quite a [...]]]></description>
			<content:encoded><![CDATA[<p>PHP syntax shows obvious similarities to Perl.  For example, we have the prefixing of variables (<code>$</code>), the arrow for object access (<code>-&gt;</code>), and semicolons terminating our statements.  While it&#8217;s perhaps less obvious to PHP developers at first, Ruby&#8217;s syntax has also been influenced by Perl in various ways.  </p>
<p>Ruby has quite a few interesting syntax features, some inspired by Perl and some not, that are sometimes lumped together as the &#8220;Perlisms&#8221; in Ruby.  Let&#8217;s explore a few of the more useful ones you&#8217;ll likely encounter in Rails applications.</p>
<h3>Word Arrays</h3>
<p>Most of us need arrays all the time in our applications.  </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #0000ff;">$days</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Mon'</span>, <span style="color: #ff0000;">'Tue'</span>, <span style="color: #ff0000;">'Wed'</span>, <span style="color: #ff0000;">'Thu'</span>, <span style="color: #ff0000;">'Fri'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

</p>
<p>We learned early in <a href="http://pragprog.com/titles/ndphpr">the book</a> that Ruby has a very convenient syntax for arrays:</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">days = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'Mon'</span>, <span style="color:#996600;">'Tue'</span>, <span style="color:#996600;">'Wed'</span>, <span style="color:#996600;">'Thu'</span>, <span style="color:#996600;">'Fri'</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

</p>
<p>However, if your array elements are words (no spaces), you can do one better.</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">days = %w<span style="color:#006600; font-weight:bold;">&#91;</span>Mon Tue Wed Thu Fri<span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

</p>
<p>The <code>%w</code> array modifier allows you to write more readable arrays of words by omitting the noise of the quotes and commas.</p>
<h3>Regular Expression Matches</h3>
<p>In PHP, regular expressions are written inside strings.  These strings are passed to functions like <code>preg_match()</code> that perform the matching:</p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #0000ff;">$text</span> = <span style="color: #ff0000;">'You have 12 new messages'</span>;
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">preg_match</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/(<span style="color: #000099; font-weight: bold;">\d</span>+) new/'</span>, <span style="color: #0000ff;">$text</span>, <span style="color: #0000ff;">$matches</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Found {$matches[1]} messages&quot;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

</p>
<p>Ruby&#8217;s regular expressions aren&#8217;t written inside strings because they have a native syntax supported by the language.  We can write the above example as:</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">text = <span style="color:#996600;">'You have 12 new messages'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> matches = /<span style="color:#006600; font-weight:bold;">&#40;</span>\d+<span style="color:#006600; font-weight:bold;">&#41;</span> new/.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>text<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Found #{matches[1]} messages&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

</p>
<p>Since <code>/(\d+) new/</code> is itself an object, we can call the <code>match</code> method on it.  The snippet otherwise looks similar to the PHP version.  </p>
<p>However, there&#8217;s another way:</p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">text = <span style="color:#996600;">'You have 12 new messages'</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Found #{$1} messages&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> text =~ /<span style="color:#006600; font-weight:bold;">&#40;</span>\d+<span style="color:#006600; font-weight:bold;">&#41;</span> new/</pre></div></div>

</p>
<p>You can use the <code>=~</code> operator to test if a regexp matches and <code>!~</code> to test if it doesn&#8217;t.  Also, Ruby automatically assigns the regexp matches to special variables.  <code>$1</code> is the first match, <code>$2</code> is the second match, and so on.  </p>
<p>Finally, we reversed the conditional to shorten the snippet a bit.  </p>
<h3>Grouping Thousands</h3>
<p>Have you ever needed a fairly large numeric literal?</p>
<p class="php label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #0000ff;">$bytes</span> = <span style="color: #cc66cc;">1048576</span>;</pre></div></div>

<p>You might appreciate that Ruby allows you to use underscores in numbers.  </p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">bytes = 1_048_576   <span style="color:#008000; font-style:italic;"># Ruby's Grouping</span>
&nbsp;
bytes = <span style="color:#006666;">1</span>.<span style="color:#9900CC;">megabyte</span>  <span style="color:#008000; font-style:italic;"># Rails' ActiveSupport</span></pre></div></div>

<p>You can use the underscore to group thousands or whatever you like, and Ruby will just filter it out.  For common numbers like byte multiples, Rails takes it to another level of readability with special methods like <code>megabyte</code> shown above.</p>
<h3>Parting Thoughts</h3>
<p>There&#8217;s quite a few other so-called Perlisms in Ruby.  Some are great, and some aren&#8217;t so great.  For example, you can access Ruby&#8217;s load path (analogous to PHP&#8217;s <code>include_path</code>) with the readable <code>$LOAD_PATH</code> variable or the cryptic <code>$:</code> variable.  <a href="http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby">This post</a> from Nick Seiger has some really obscure features of Ruby.  </p>
<p>Ruby&#8217;s language design makes programming very convenient, but it&#8217;s power is that its allows us to write intuitive programs that often read like natural language.  When you abuse the syntax features, you only make code harder to read.</p>
<p>Experienced PHP developers learn that there are some aspects of the PHP language, deprecated or not, that you might want to avoid for cleaner code.  This is sometimes true of Ruby also, as with most programming languages.</p>
<p>While the features demonstrated above are useful and popular, some features of Ruby aren&#8217;t popular in the Rails community at all.  Most of the <a href="http://www.zenspider.com/Languages/Ruby/QuickRef.html#18">pre-defined variables</a> that aren&#8217;t words fall into this category.  In general, Rails developers always strive to write programs that are easier to understand and maintain.  </p>
<p>Becoming an effective Rails developer is not only learning the features of the Ruby language, but developing a sense of good taste for how to use those features.</p>
]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2008/01/03/useful-perlisms-in-ruby/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP Object Attributes</title>
		<link>http://railsforphp.com/2007/12/21/accessing-attributes-in-php-objects/</link>
		<comments>http://railsforphp.com/2007/12/21/accessing-attributes-in-php-objects/#comments</comments>
		<pubDate>Fri, 21 Dec 2007 19:40:07 +0000</pubDate>
		<dc:creator>Derek DeVries</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://railsforphp.com/2007/12/21/accessing-attributes-in-php-objects/</guid>
		<description><![CDATA[In this article we&#8217;re going to draw inspiration from Ruby&#8217;s attr_accessor method in an attempt to create dynamic getter and setter methods for PHP classes. Let&#8217;s start by taking a look at a similar way in which we can use the two languages to create a simple Book class. 
Accessing Attributes
We can directly assign and [...]]]></description>
			<content:encoded><![CDATA[<p>In this article we&#8217;re going to draw inspiration from Ruby&#8217;s <code>attr_accessor</code> method in an attempt to create dynamic getter and setter methods for PHP classes. Let&#8217;s start by taking a look at a similar way in which we can use the two languages to create a simple Book class. </p>
<h3>Accessing Attributes</h3>
<p>We can directly assign and access public data members in PHP, but Ruby objects have no public data members. We can simulate a similar behavior in Ruby using the <code>attr_accessor</code> method.</p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Book
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #0000ff;">$author</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$book</span> = <span style="color: #000000; font-weight: bold;">new</span> Book;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">author</span> = <span style="color: #ff0000;">'jim'</span>;
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">author</span>;
<span style="color: #808080; font-style: italic;">// =&gt; jim</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Book
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:author</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
book = Book.<span style="color:#9900CC;">new</span>
book.<span style="color:#9900CC;">author</span> = <span style="color:#996600;">'jim'</span>
<span style="color:#CC0066; font-weight:bold;">print</span> book.<span style="color:#9900CC;">author</span>
<span style="color:#008000; font-style:italic;"># =&gt; jim</span></pre></div></div>

<p>While it appears that Ruby is using a public attribute, this is simply because parentheses are optional in Ruby. We can call the same methods like this: </p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">book.<span style="color:#9900CC;">author</span>=<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'jim'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">print</span> book.<span style="color:#9900CC;">author</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Taking a look at the class definition, one might infer that Ruby&#8217;s <code>attr_accessor</code> class method is creating a public attribute. It is actually doing something quite different. When we invoke this method, we dynamically create the <code>author()</code> and <code>author=()</code> instance methods which access and assign the <code>@author</code> instance variable in our object. This gives us some flexibility to change the implementation of these methods later on if we choose. </p>
<h3>The Problem with Public</h3>
<p>Imagine we want our class to capitalize the author name when we assign it. In Ruby, we could implement this pretty easily without breaking the existing API. We can explicitly define the accessor method ourselves. </p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Book
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:author</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> author=<span style="color:#006600; font-weight:bold;">&#40;</span>author<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@author</span> = author.<span style="color:#9900CC;">capitalize</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
book = Book.<span style="color:#9900CC;">new</span>
book.<span style="color:#9900CC;">author</span> = <span style="color:#996600;">'jim'</span>
<span style="color:#CC0066; font-weight:bold;">print</span> book.<span style="color:#9900CC;">author</span>
<span style="color:#008000; font-style:italic;"># =&gt; Jim</span></pre></div></div>

<p>If we were to try and do the same thing in PHP with public data members, we immediately see a problem. We can&#8217;t change the implementation of author assignment. We are assigning the value directly, and have no encapsulation within the object. </p>
<h3>Getter and Setters</h3>
<p>There is a point where you basically give up on public attributes for all but the most basic data structures. They&#8217;re just too inflexible, with the preferred alternative being protected or private members with getter and setter methods: </p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Book
<span style="color: #66cc66;">&#123;</span>
    protected <span style="color: #0000ff;">$_author</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setAuthor<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$author</span><span style="color: #66cc66;">&#41;</span> 
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$this</span>-&gt;_author = <span style="color: #000066;">ucfirst</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$author</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getAuthor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;_author;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$book</span> = <span style="color: #000000; font-weight: bold;">new</span> Book;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">setAuthor</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'jim'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">getAuthor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// =&gt; Jim</span></pre></div></div>

<p>This class definition accomplishes the same thing as the above Ruby class definition. By using methods to access the author, we can easily abstract any details within our class without any need to change the class API in the future. </p>
<h3>Better Access using Magic</h3>
<p>The PHP example is a bit more verbose than the Ruby, and would become even more so for each attribute that we add. Using Ruby as an example, we can actually come up with a reasonably elegant solution to the repetitive getter/setter methods using PHP&#8217;s <code>__get</code> and <code>__set</code> magic methods. Let&#8217;s say that we also want a title and page count for our class. The Ruby definition would look like this: </p>
<p class="ruby label">Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Book
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:author</span>, <span style="color:#ff3333; font-weight:bold;">:title</span>, <span style="color:#ff3333; font-weight:bold;">:pages</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> author=<span style="color:#006600; font-weight:bold;">&#40;</span>author<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@author</span> = author.<span style="color:#9900CC;">capitalize</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
book = Book.<span style="color:#9900CC;">new</span>
book.<span style="color:#9900CC;">author</span> = <span style="color:#996600;">'jim'</span>
book.<span style="color:#9900CC;">title</span>  = <span style="color:#996600;">'Wandering in the Desert'</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;#{book.author} : #{book.title}&quot;</span>
<span style="color:#008000; font-style:italic;"># =&gt; Jim : Wandering in the Desert</span></pre></div></div>

<p>Wouldn&#8217;t it be nice if if we could do something like this in PHP?</p>
<p class="php label">PHP</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Book <span style="color: #000000; font-weight: bold;">extends</span> AttrObject
<span style="color: #66cc66;">&#123;</span>
    protected <span style="color: #0000ff;">$_author</span>;
    protected <span style="color: #0000ff;">$_title</span>;
    protected <span style="color: #0000ff;">$_pages</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">attrAccessor</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'author'</span>, <span style="color: #ff0000;">'title'</span>, <span style="color: #ff0000;">'pages'</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setAuthor<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$author</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$this</span>-&gt;_author = <span style="color: #000066;">ucfirst</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$author</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$book</span> = <span style="color: #000000; font-weight: bold;">new</span> Book;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">author</span> = <span style="color: #ff0000;">'jim'</span>;
<span style="color: #0000ff;">$book</span>-&gt;<span style="color: #006600;">title</span>  = <span style="color: #ff0000;">'Wandering in the Desert'</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;$book-&gt;author : $book-&gt;title&quot;</span>;
<span style="color: #808080; font-style: italic;">// =&gt; Jim : Wandering in the Desert</span></pre></div></div>

<p>This syntax simplifies our class by eliminating redundant getter and setter methods. The custom <code>AttrObject</code> class defines three methods. The  <code>attrAccessor()</code> method creates an attribute on the instance that can be both read and written. Two other variations exist: <code>attrReader()</code> creates attributes that can only be read, and <code>attrWriter()</code> creates attributes that can only be written.</p>
<h3>Overriding Attributes</h3>
<p>While it may appear that we&#8217;re calling public attributes, these attributes remain protected. <code>AttrObject</code> works by using the missing property handlers <code>__get()</code> and <code>__set()</code>. When one of our attributes is accessed, it first scans for a method like <code>getAuthor()</code> or <code>setAuthor()</code> on the object. If not found, it looks for a protected property like <code>$_author</code>. If that is also not found, it searches for a method called <code>_get()</code> or <code>_set()</code>. Finally, an error is found if nothing is available to satisfy the conditions. </p>
<p>You can download the class source code here:<br />
<a href='/wp-content/uploads/2007/12/attr-code.tgz' title='attr-code.tgz for Unix'>attr-code.tgz for Unix</a><br />
<a href='/wp-content/uploads/2007/12/attr-code.zip' title='attr-code.zip for Windows'>attr-code.zip for Windows</a></p>
<p>The advantages of the example are clear when the implementation of these attributes need to change. The attributes promote encapsulation by allowing the implementation to change while the interface remains the same.</p>
]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2007/12/21/accessing-attributes-in-php-objects/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Beta 2 Released</title>
		<link>http://railsforphp.com/2007/12/11/beta-2-released/</link>
		<comments>http://railsforphp.com/2007/12/11/beta-2-released/#comments</comments>
		<pubDate>Tue, 11 Dec 2007 20:21:18 +0000</pubDate>
		<dc:creator>Mike Naberezny</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://railsforphp.com/2007/12/11/beta-2-released/</guid>
		<description><![CDATA[The second beta of Rails for PHP Developers has been released!  If you&#8217;ve already purchased the book, you can visit the &#8220;Your Account&#8221; page on the Pragmatic Bookshelf website to generate the new PDF at any time.  
The biggest change is that the beta now includes the rest of the chapters for Part [...]]]></description>
			<content:encoded><![CDATA[<p>The second beta of <a href="http://pragprog.com/titles/ndphpr">Rails for PHP Developers</a> has been released!  If you&#8217;ve already purchased the book, you can visit the &#8220;Your Account&#8221; page on the Pragmatic Bookshelf website to generate the new PDF at any time.  </p>
<p>The biggest change is that the beta now includes the rest of the chapters for Part 1, &#8220;From PHP to Rails&#8221;.  These include many new pages from &#8220;Beginning Ruby Code&#8221; to &#8220;Embracing the Ruby Philosophy&#8221;.</p>
<p>We&#8217;ve also been tracking the changes in the Rails 2.0 pre-release closely.  You&#8217;ll find we&#8217;ve made many adjustments to the book for new features and changes in Rails.</p>
<p>You might also notice that we&#8217;ve made many other small corrections and additions since the first beta.  Thanks for all of your praise and the tremendous amount of great feedback you&#8217;ve sent!</p>
]]></content:encoded>
			<wfw:commentRss>http://railsforphp.com/2007/12/11/beta-2-released/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 2.061 seconds -->
<!-- Cached page served by WP-Cache -->
