<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Developing With Style</title>
	
	<link>http://developingwithstyle.com</link>
	<description>A Weblog written, styled and hacked by Joel Moss</description>
	<pubDate>Tue, 09 Jun 2009 17:58:16 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/DevelopingWithStyle" type="application/rss+xml" /><item>
		<title>10 Reasons why Ruby is better than PHP - Reason #5</title>
		<link>http://feedproxy.google.com/~r/DevelopingWithStyle/~3/c5N7sxN72gI/</link>
		<comments>http://developingwithstyle.com/2009/06/09/10-reasons-why-ruby-is-better-than-php-reason-5/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 17:58:16 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://developingwithstyle.com/?p=380</guid>
		<description><![CDATA[It&#8217;s been a while I know, and I am aware that some of you are chomping at the bit to read reason #5. Apologies for the wait, but some of us have work to do, and a mammoth side project to complete, which by the way, is coming along nicely.
Before I start on reason #5, [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while I know, and I am aware that some of you are chomping at the bit to read reason #5. Apologies for the wait, but some of us have work to do, and a <a href="http://codaset.com">mammoth side project</a> to complete, which by the way, is coming along nicely.</p>
<p>Before I start on reason #5, I want to mention a couple of posts in a similar vein to my series.</p>
<p>Andy Jeffries &#8220;<a href="http://andyjeffries.co.uk/articles/4-reasons-why-ruby-syntax-is-better-than-phps-">4 Reasons why Ruby syntax is better than PHP&#8217;s</a>&#8221; is a straight to the point article all about the strengths of Ruby&#8217;s syntax over PHP. If after reading his post, you still think syntax doesn&#8217;t matter, then you are a lost cause, and beyond help of any kind!</p>
<p>Another post I actually read a while ago, but forgot all about until I was <a href="http://twitter.com/m3nt0r/status/2091044381">pleasantly reminded of</a>, is Bitcetera&#8217;s &#8220;<a href="http://www.bitcetera.com/en/techblog/2009/04/07/10-reasons-why-php-is-still-better-than-ruby–/">10 Reasons why PHP is still better than Ruby</a>&#8220;. Read it carefully all the way through, as it is a great read.</p>
<p>Now, we get to Modules in Ruby, again, a feature that has no direct equivalent in PHP. Modules are very similar to classes, in fact they are written in much the same way:</p>
<p><code>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw1">module</span> Animal</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> name</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&quot;Dog&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>So what are they used for? Well, modules are generally used for two purposes; creating namespaces and mixins. I&#8217;m sure (and I hope) that you all know what namespaces are. But you probably have no idea what a mixin is. Well I&#8217;ll get to that in a bit. Let&#8217;s quickly talk about how Ruby uses namespaces.</p>
<p>Let us imagine that we have been charged with cleaning up and re-organizing our code, and we have a lot of it. Normally in PHP you might simply stick this code in a file and include it wherever you need it, but what if you have two methods or constants that are named the same? Some developers have worked around this by separating different class package names with underscores. This solution solves the immediate problem and also works conveniently with autoloading in PHP. It can, however, leave us with some really long class names.</p>
<p>Let&#8217;s imagine we have two classes that define a <code>Document</code> class. In PHP we would probably prefix each class name with the document type:</p>
<p><code>
<div class="geshi no php">
<div class="head">PHP</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> XML_Document <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">print</span> <span class="st0">&quot;new xml document<span class="es0">\n</span>&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> PDF_Document <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">print</span> <span class="st0">&quot;new pdf document<span class="es0">\n</span>&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$xml</span> <span class="sy0">=</span> <span class="kw2">new</span> XML_Document<span class="sy0">;</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$pdf</span> <span class="sy0">=</span> <span class="kw2">new</span> PDF_Document<span class="sy0">;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>Ruby would achieve the same thing like this:</p>
<p><code>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw1">module</span> XML </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">class</span> Document </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">def</span> initialize </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw3">puts</span> <span class="st0">&#39;new xml document&#39;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">end</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">module</span> PDF </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">class</span> Document </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">def</span> initialize </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw3">puts</span> <span class="st0">&#39;new pdf document&#39;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">end</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span> </div>
</li>
<li class="li1">
<div class="de1">xml = <span class="re2">XML::Document</span>.<span class="me1">new</span> </div>
</li>
<li class="li1">
<div class="de1">pdf = <span class="re2">PDF::Document</span>.<span class="me1">new</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>Now I know that the as-yet-to-be-released PHP 5.3 will introduce namespaces, but come on! <a href="http://php.net/manual/language.namespaces.rationale.php">Have you actually seen how PHP namespaces are created and written?!</a> WTF!? Using slashes just looks wrong! Slashes are used in directory paths, and that is where they should stay. And why should I have to declare my namespace at the very top of the file and no where else?</p>
<p>But the best thing about Ruby&#8217;s modules, is the ability to mix them into my classes, as if it was part of the original class code. Think of it as inheritance, but better. Everyone knows that in PHP and Ruby, a class can only inherit from one class at a time. To inherit from another class would involve you having to create a complicated chain of inheritance. Mixins eliminate that need. Just create a class, inherit from another class, and then mixin as many modules as you want.</p>
<p><code>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw1">module</span> Movement </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> run </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">puts</span> <span class="st0">&quot;I&#39;m running!&quot;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> walk </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">puts</span> <span class="st0">&quot;I&#39;m walking a bit briskly!&quot;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> crawl </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">puts</span> <span class="st0">&quot;I&#39;m so slowwww!&quot;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">class</span> Man </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">include</span> Movement </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> jump </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">puts</span> <span class="st0">&quot;I&#39;m bipedal and I can jump like a fool!&quot;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">class</span> Sloth </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">include</span> Movement </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> flop </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">puts</span> <span class="st0">&quot;It&#39;s all a lie&#8230;all I can do is flop around.&quot;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">mister_man = Man.<span class="me1">new</span> </div>
</li>
<li class="li1">
<div class="de1">mister_man.<span class="me1">run</span> </div>
</li>
<li class="li1">
<div class="de1">→ &nbsp;I<span class="st0">&#39;m running!</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="st0">mister_slothy = Sloth.new </span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">mister_slothy.flop </span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">→ &nbsp;It&#39;</span>s all a lie&#8230;<span class="me1">all</span> I can <span class="kw1">do</span> is flop around.</div>
</li>
</ol>
</div>
<p></code></p>
<p>As you can see, this mechanism is very similar to inheritance in that you can use all of the mixin&#8217;s code.  To use a mixin, simply define a module and then use the include keyword followed by the module&#8217;s name (note I said module; the include keyword has nothing to do with files or libraries like in PHP); from then on the class has access to all of that module&#8217;s constants and methods.</p>
<p>The above example really doesn&#8217;t do mixins justice, but it&#8217;s a very simple example to help demonstrate how they work. Modules are a very versatile tool in Ruby and allow us to extract and share common behavior in objects, and are another reason why Ruby is better than PHP (in my opinion).</p>
<img src="http://feeds.feedburner.com/~r/DevelopingWithStyle/~4/c5N7sxN72gI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://developingwithstyle.com/2009/06/09/10-reasons-why-ruby-is-better-than-php-reason-5/feed/</wfw:commentRss>
		<feedburner:origLink>http://developingwithstyle.com/2009/06/09/10-reasons-why-ruby-is-better-than-php-reason-5/</feedburner:origLink></item>
		<item>
		<title>10 Reasons why Ruby is better than PHP - Reason #4</title>
		<link>http://feedproxy.google.com/~r/DevelopingWithStyle/~3/yU31ZbH6hdE/</link>
		<comments>http://developingwithstyle.com/2009/06/01/10-reasons-why-ruby-is-better-than-php-reason-4/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 22:17:31 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://developingwithstyle.com/?p=374</guid>
		<description><![CDATA[I bet you thought I&#8217;d never get any more reasons out did ya? Well, today, I&#8217;m going to tell you all about IRB and the Rails command line debugger, both of which I use religiously.
One of the great pleasures of using Ruby, apart from the previous three reasons, and of course the following five reasons, [...]]]></description>
			<content:encoded><![CDATA[<p>I bet you thought I&#8217;d never get any more reasons out did ya? Well, today, I&#8217;m going to tell you all about IRB and the Rails command line debugger, both of which I use religiously.</p>
<p>One of the great pleasures of using Ruby, apart from the <a href="http://developingwithstyle.com/2009/05/20/10-reasons-why-ruby-is-better-than-php-reason-1/">previous</a> <a href="http://developingwithstyle.com/2009/05/21/10-reasons-why-ruby-is-better-than-php-reason-2/">three</a> <a href="http://developingwithstyle.com/2009/05/27/10-reasons-why-ruby-is-better-than-php-reason-3/">reasons</a>, and of course the following five reasons, is the wonder that is the Interactive Ruby console, or IRB. IRB is an interactive interpreter; which means that instead of processing a file, it processes what you type in during a session. It&#8217;s a great tool for testing Ruby code, and a great tool for learning Ruby. </p>
<p>Once you have Ruby installed on your computer, you will notice that the <code>ruby</code> command will do the same thing as PHP - it silently waits for a script to be streamed in on <code>stdin</code>. On it&#8217;s own, that&#8217;s not particularly friendly, or even that useful. But another command that is installed alongside Ruby, is the <code>irb</code> command. Start up your command line tool of choice, and simply enter these three immortal letters: <code>irb</code>. Then you will see this:</p>
<p><code>
<pre>irb></pre>
<p></code></p>
<p>This is the <code>irb</code> prompt. Now all you do is simply type any ruby code, and it will show you the result of each and every expression as it is evaluated. Not sure how a method works, or can&#8217;t remember the output of a method? Then test it with IRB.</p>
<p>IRB lets you do things without having to fake out your controller. For example, in PHP, I&#8217;m forever using <code>echo</code> or CakePHP&#8217;s <code>debug</code> function to print out some useful info to the browser when debugging my code. But when developing with Ruby, I simply have my trusty IRB prompt ready and waiting for me to inflict world domination.</p>
<p><code>
<pre>>> @george = Person.find_by_name('George')
>> @bob = Person.find_by_name('Bob')
>> @bob.friends << @george</pre>
<p></code></p>
<p>Each of the above lines will be following by IRB echoing the result. So I can see exactly what is going on, and without me having to echo out data all through my code and to the browser.</p>
<p>This is actually very, very useful, and is a great way to test your code, or in fact any code at all. It&#8217;s even better if you are a command line freak like me.</p>
<p>What is even better, better (!?) is that Rails also has its own command line interpreter, which is basically an extension of IRB. What makes it a little different, is that when it is started up, it also loads all your Rails code. So you have instant access to your Rails models, which is also very handy. Just cd to your Rails app root, and run:</p>
<p><code>
<pre>script/console</pre>
<p></code></p>
<p>You are then shown a similar command line prompt to IRB, and can run any part of your Rails app code.</p>
<p>To wrap up reason #4, I want to tell you about one of the best things about developing with Ruby on Rails. It&#8217;s another extension of IRB, similar to Rails <code>script/console</code>, but is simply genius when debugging your Rails code.</p>
<p>There are several ways in which you can run your Rails app, but the traditional method is by using the the <code>script/server</code> script within your app. When run on the command line, this will start up a small web server, usually run by Webrick or Mongrel. Now I wouldn&#8217;t recommend this method when running your app in production, as <a href="http://www.modrails.com/">there are much better, and more efficient ways to do so</a>, but I use this all the time when developing or testing my app code.</p>
<p>Just <code>cd</code> to your Rails app root and run this on the command line:</p>
<p><code>
<pre>script/server -u</pre>
<p></code></p>
<p>Notice I appended a command line flag; <code>-u</code>. This tells the Rails server to start up in Debug mode. If you use Mongrel, this is what you will see:</p>
<p><code>
<pre>=> Booting Mongrel
=> Rails 2.3.2 application starting on http://0.0.0.0:3000
=> Debugger enabled
=> Call with -d to detach
=> Ctrl-C to shutdown server</pre>
<p></code></p>
<p>And the command line prompt will sit there blinking at you, waiting for you to load your app in your browser. So don&#8217;t keep it waiting, open up your browser, and go to <code>http://localhost:3000</code>, and you will be shown your Rails app in all its glory.</p>
<p>Now we get to the good bit. Open up the code of any of your controllers in your favourite IDE or <a href="http://macromates.com">text editor</a>. Then type the word <code>debugger</code> within one of your actions. Then go back your browser and navigate to that page. You should the notice that the page does not appear to finish loading. That is because your <code>debugger</code> keyword within the action method, has paused the server until you tell it to continue.</p>
<p>Go back to your command line when you started the Rails server, and you will see a bunch of log data, following by what looks like an IRB prompt. That is effectively what it is. You can now type any Ruby code at that prompt, just like you would with IRB or <code>script/console</code>. But the beauty of this, is that you have access to all the variables and methods at exactly the same point in your code where you placed the <code>debugger</code> keyword.</p>
<p>So instead of having to type echo or print statements throughout your code, and running them through your browser to see the results. You can add the debugger keywork at the point where you want to debug, and use the command line to access your code in real time.</p>
<p>I really hope you got all that, as this feature has saved me hours and hours of time when debugging my code. I can honestly say that it is a life saver. You really should try it out now, or if you&#8217;re a little impatient, <a href="http://tryruby.hobix.com/">try it out in your browser for instant gratification</a>. You should also check out Amy Hoy&#8217;s &#8220;<em><a href="http://slash7.com/articles/2006/12/21/secrets-of-the-rails-console-ninjas">Secrets of the Rails Console Ninja&#8217;s</a></em>&#8220;.</p>
<p>Thanks again for sticking with me, and hold on to your pants for reason #5 where I will be talking about Modules; Ruby&#8217;s answer to namespaces. Which will lead me nicely onto another Ruby exclusive: Mixins!</p>
<p>P.S. I really appreciate everyone&#8217;s comments. Keep them coming.</p>
<img src="http://feeds.feedburner.com/~r/DevelopingWithStyle/~4/yU31ZbH6hdE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://developingwithstyle.com/2009/06/01/10-reasons-why-ruby-is-better-than-php-reason-4/feed/</wfw:commentRss>
		<feedburner:origLink>http://developingwithstyle.com/2009/06/01/10-reasons-why-ruby-is-better-than-php-reason-4/</feedburner:origLink></item>
		<item>
		<title>10 Reasons why Ruby is better than PHP - Reason #3</title>
		<link>http://feedproxy.google.com/~r/DevelopingWithStyle/~3/fxsfdlZUbeU/</link>
		<comments>http://developingwithstyle.com/2009/05/27/10-reasons-why-ruby-is-better-than-php-reason-3/#comments</comments>
		<pubDate>Wed, 27 May 2009 10:28:10 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://developingwithstyle.com/?p=364</guid>
		<description><![CDATA[So onwards and upwards! It appears that I still have a little work to do to convince you non-believers, so lets try again&#8230;
As previously mentioned, Ruby has some features that simply cannot be found in many other languages, let alone PHP. So here I introduce yet another one, and this is a biggie. But, please [...]]]></description>
			<content:encoded><![CDATA[<p>So onwards and upwards! It appears that I still have a little work to do to convince you non-believers, so lets try again&#8230;</p>
<p>As <a href="http://developingwithstyle.com/2009/05/21/10-reasons-why-ruby-is-better-than-php-reason-2">previously mentioned</a>, Ruby has some features that simply cannot be found in many other languages, let alone PHP. So here I introduce yet another one, and this is a biggie. But, please be warned that this one is a little harder to explain, so please be bear with me as I attempt to enlighten you.</p>
<p>Some of you may have heard of a language feature known in computer science as <em>closures</em>. If you have done any Javascript development, you should be familiar with closures, but may not know them by name. Ruby uses closures extensively, but refers to them simply as <em>blocks</em>. Which is actually easier to understand.</p>
<p>Many developers seem to think that blocks are somewhat of an obscure language feature, as they are pretty hard to find an equivalent in other languages. PHP is not the only language without support for blocks, but you should understand them, as they can be very useful, and quite powerful.</p>
<p>Although there is no direct translation of blocks in PHP, we can make some analogies to PHP that will help us understand and become familiar with blocks, and how they are useful. Lets use PHP to start a family:</p>
<p><code>
<div class="geshi no php">
<div class="head">PHP</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> start_family<span class="br0">&#40;</span><span class="re1">$num</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">echo</span> <span class="st0">&quot;There are now $num kids in my family&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p><code>
<div class="geshi no php">
<div class="head">PHP</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span><span class="re1">$kids</span> <span class="sy0">=</span> <span class="nu0">99</span><span class="sy0">;</span> <span class="re1">$kids</span> <span class="sy0">&amp;</span>gt<span class="sy0">;</span> <span class="nu0">0</span><span class="sy0">;</span> <span class="re1">$kids</span><span class="sy0">&#8211;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; start_family<span class="br0">&#40;</span><span class="re1">$kids</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>When the above code is executed, PHP evaluates the condition in the for loop, which calls the <code>start_family</code> function on each iteration.</p>
<p>The <code>for</code> and <code>function</code> keywords are built-in constructs of PHP, in the same way that semi-colons and curly braces are built-in. But there are very few control constructs in PHP that allow code to be attached to them within the curly braces. It&#8217;s not actually possible to add or create new control constructs in PHP, unless of course you fancy whipping up a PHP extension in C. (eurghh!)</p>
<p>To be fair, we could condense the above into the following, as there really would be no need to use a function within the for loop.</p>
<p><code>
<div class="geshi no php">
<div class="head">PHP</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span><span class="re1">$num</span> <span class="sy0">=</span> <span class="nu0">99</span><span class="sy0">;</span> <span class="re1">$num</span> <span class="sy0">&amp;</span>gt<span class="sy0">;</span> <span class="nu0">0</span><span class="sy0">;</span> <span class="re1">$num</span><span class="sy0">&#8211;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">echo</span> <span class="st0">&quot;There are now $num kids in my family&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>So now lets do the same thing in Ruby:</p>
<p><code>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1"><span class="nu0">99</span>.<span class="me1">downto</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="sy0">|</span>num<span class="sy0">|</span> <span class="kw3">puts</span> <span class="st0">&quot;There are now #{$num} kids in my family&quot;</span> <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>Slightly unfamilar syntax, but that&#8217;s nothing new when comparing Ruby with PHP. But the above is actually quite declarative. Lets break this down a little.</p>
<p>Hopefully, if you read <a href="http://developingwithstyle.com/2009/05/20/10-reasons-why-ruby-is-better-than-php-reason-1/">reason #1</a> you will now know that <code>99</code> is an object, which has a method called <code>downto</code>. It&#8217;s pretty obvious that this method simply counts down from 99 to 1, and iterates through each number. It serves roughly the same purpose as the <code>for</code> construct in our PHP example. The odd looking bit between the curly braces, with the goalposts, is the block.</p>
<p>There are many similarities between the Ruby block and the PHP <code>for</code> loop. This is because the Ruby block is actually just a function in another form. A block is a function without a name, or an anonymous function. Yes I know PHP 5.3 will include some sort of support for anonymous functions, but again, it&#8217;s an afterthought.</p>
<p>A Ruby block can receive parameters in much the same way a method or PHP function can, but they are placed within the goalposts of the block. In our case, we place the number (<code>num</code>) of kids between the goal posts, and this becomes our one and only parameter. We can pass more parameters to the block, by simply separating them with a comma. If we don&#8217;t want to pass any parameters, then we leave off the goalposts completely. The rest of the block is executable code, just like any other Ruby method.</p>
<p>Every method in Ruby, whether it be a built-in one, or one that you create, can be passed a block. And that one simple fact is what makes blocks so powerful. In fact, if the <code>downto</code> method were written in pure ruby, it could look something like this:</p>
<p><code>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw1">class</span> <span class="kw3">Integer</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> downto<span class="br0">&#40;</span>value, <span class="sy0">&amp;</span>block<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; n = <span class="kw2">self</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">while</span> n <span class="sy0">&gt;</span>= value</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; block.<span class="me1">call</span> n</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; n <span class="sy0">-</span>= <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">return</span> <span class="kw2">self</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>So we <a href="http://developingwithstyle.com/2009/05/21/10-reasons-why-ruby-is-better-than-php-reason-2/">reopen</a> the <code>Integer</code> class, and redeclare the <code>downto</code> method. We then pass two parameters to that method. Pay particular attention to the second parameter: <code>&#038;block</code>, as it is very important.</p>
<p>When a block is passed to a Ruby method, it can be captured in a variable. In fact, the code that we pass within the block to <code>downto</code> is converted into an object, and then stored inside a variable. The ampersand <code>&#038;block</code> tells Ruby to store this block in a variable called <code>block</code>. The keyword <code>self</code> in this example refers to the object itself, which in this case is the integer <code>99</code>.</p>
<p>Still with me? Good! Lets take our earlier example again, and apply it to the <code>downto</code> method that we just created:</p>
<p><code>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1"><span class="nu0">99</span>.<span class="me1">downto</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span> <span class="kw1">do</span> <span class="sy0">|</span>num<span class="sy0">|</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">puts</span> <span class="st0">&quot;There are now #{$num} kids in my family&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>Hold a second, the above code is different to our first example isn&#8217;t it? Yes it is, well spotted! Just as in everything in Ruby, you don&#8217;t need the curly braces. The use of the <code>do</code> keyword is usually used for larger blocks, that need to span multiple lines. But it does exactly the same thing.</p>
<p>When the <code>downto</code> method is called, it receives two parameters: <code>value</code> contains the number of kids we have, and <code>block</code> contains the block of code that will be run on each iteration.</p>
<p>The inside of our <code>downto</code> method should look familiar to you, as it is effectively doing the same thing as our PHP <code>for</code> loop. It just counts down from 99 to 1, using a <code>while</code> loop. Then on each iteration of the <code>while</code> loop, it yields control to the code that is passed in the <code>block</code> variable, by calling the <code>block.call</code> method. Any parameters passed to <code>block.call</code>, are passed in the same way to the goalposts in the block.</p>
<p>A little confusing I know, but I really wanted to explain how blocks work as well as I could, as they are becoming ever more useful to me, the more that I develop with Ruby.</p>
<p>To finish off, I really want to show you an awesome way that Ruby&#8217;s blocks are used. I won&#8217;t go into it too much though, as it leads on to another of my reasons. There is a Ruby library (Gem) called <em><a href="http://www.thoughtbot.com/projects/shoulda/">Shoulda</a></em> which improves the <em>Test:Unit</em> unit testing library that is supported as standard in Ruby on Rails. Shoulda improves the language of your unit tests, and turns them into an almost human readable form. So here we have a test:</p>
<p><code>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1">context <span class="st0">&quot;a User instance&quot;</span> <span class="kw1">do</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; should <span class="st0">&quot;return its full name&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; assert_equal <span class="st0">&#39;John Doe&#39;</span>, user.<span class="me1">full_name</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>Now start from the beginning of that code and just read it out load. It may end up something like this &#8220;<em>a User instance should return its full name</em>&#8220;. Of course there is more in between, but what I am trying to convey is how expressive and readable the above code is. And that is because of the power of blocks.</p>
<p>Hope that helps a little. Keep the comments coming guys, and thanks for reading.</p>
<img src="http://feeds.feedburner.com/~r/DevelopingWithStyle/~4/fxsfdlZUbeU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://developingwithstyle.com/2009/05/27/10-reasons-why-ruby-is-better-than-php-reason-3/feed/</wfw:commentRss>
		<feedburner:origLink>http://developingwithstyle.com/2009/05/27/10-reasons-why-ruby-is-better-than-php-reason-3/</feedburner:origLink></item>
		<item>
		<title>10 Reasons why Ruby is better than PHP - Reason #2</title>
		<link>http://feedproxy.google.com/~r/DevelopingWithStyle/~3/SHgVYVnpt0c/</link>
		<comments>http://developingwithstyle.com/2009/05/21/10-reasons-why-ruby-is-better-than-php-reason-2/#comments</comments>
		<pubDate>Thu, 21 May 2009 19:42:12 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://developingwithstyle.com/?p=361</guid>
		<description><![CDATA[Well now then. My last post, or the comments in it, seemed to have stirred up a little hornets nest. I had my fair share of supporters and my fair share of thanks for my writings, but I also received a little criticism. I think I&#8217;ve covered most of those in the comments and also [...]]]></description>
			<content:encoded><![CDATA[<p>Well now then. <a href="http://developingwithstyle.com/2009/05/20/10-reasons-why-ruby-is-better-than-php-reason-1/">My last post</a>, or the comments in it, seemed to have stirred up a little hornets nest. I had my fair share of supporters and my fair share of thanks for my writings, but I also received a little criticism. I think I&#8217;ve covered most of those in the <a href="http://developingwithstyle.com/2009/05/20/10-reasons-why-ruby-is-better-than-php-reason-1/#comments">comments</a> and also on <a href="http://twitter.com/joelmoss">Twitter</a>, so I won&#8217;t say much more, other than to simply remind you all what I am trying to do.</p>
<p>In this little series of blog posts, my objective was to be as fair as possible when explaining my reasons why I think Ruby is better than PHP. But at the same time, this is not a comparison. Which means I won&#8217;t be pointing out PHP&#8217;s strong points - of which there are many. Also, this is my opinion, and no one elses, and I am entitled to that. And so to, are you guys.</p>
<p>And one last thing before I start reason #2. I want to say thanks to <a href="http://www.littlehart.net/atthekeyboard/2009/05/21/laziness-vs-efficiency/">Chris Hartjes for his blog post</a> earlier today. I made a comment on Twitter about lazy coders, being good coders, and he basically put me to rights, and explained what I was saying in much better terms, than I could ever do. But also, I do think he was a little harsh on me. Hear my other reasons first Chris. No hard feelings though.</p>
<p>Back to business. In todays post, I wanted to give more of a solid reason why I think Ruby excels over PHP. So I picked one that simply cannot be matched in any way by PHP. Add probably won&#8217;t ever be. But I would love to see this being made possible.</p>
<p><strong>Re-opening in Classes is soooo Useful!</strong></p>
<p>Both languages support the creation of classes as part of their object-oriented design. And they do so in much the same way. Both languages allow you to set method visibility with the use of <code>public</code>, <code>protected</code> and <code>private</code> declarations, and behave accordingly. Classes are written in a similar fashion:</p>
<p><code>
<div class="geshi no php">
<div class="head">PHP</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Person <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> greeting<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#39;Hello you!&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p><code>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw1">class</span> Person</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> greeting</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&#39;Hello you!&#39;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>Very similar, but let me explain one thing. In the PHP example above, I returned the string &#8220;<code>Hello you!</code>&#8221; by prefixing it with the <code>return</code> keyword. Like this:</p>
<p><code>
<div class="geshi no php">
<div class="head">PHP</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw1">return</span> <span class="st0">&#39;Hello you!&#39;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>But in the Ruby example, you may have noticed that there is no <code>return</code> keyword before the &#8220;<code>Hello you!</code>&#8221; string. It was just this:</p>
<p><code>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1"><span class="st0">&#39;Hello you!&#39;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>That works perfectly fine, and the &#8220;<code>Hello you!</code>&#8221; string will still be returned when calling the <code>greeting</code> method of the <code>Person</code> class. That is because Ruby will return the value from the last line of the method. You can use <code>return</code> if you want to, but you don&#8217;t have to.</p>
<p>So we&#8217;ve determined that writing classes is pretty much on the par in both PHP and Ruby. But, you may have accidentally redeclared a class in PHP, and received a nasty error:</p>
<p><code>
<div class="geshi no php">
<div class="head">PHP</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Person <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> greeting<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#39;Hello you!&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Person <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> greeting<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#39;Hey, hey, hey!&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>Doing the above will give tell you <code>PHP Fatal error: Cannot redeclare class Person</code>. Everyone knows that you just cannot do that with PHP. And to be honest it seems to make sense for it to prevent this from happening. But Ruby actually allows you to do this. In fact, you are almost encouraged to reopen classes if and when needed.</p>
<p><code>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw1">class</span> Person</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> greeting</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&#39;Hello you!&#39;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">class</span> Person</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> greeting</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&#39;Hey, hey, hey!&#39;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>In the above case, no error will be returned. Instead, Ruby will simply redefine the <code>greeting</code> method in the first class, with the same method from the second class. So you get this:</p>
<p><code>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw3">p</span> = Person.<span class="me1">new</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">puts</span> <span class="kw3">p</span>.<span class="me1">greeting</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>Which returns &#8220;<code>Hey, hey, hey!</code>&#8221; and not &#8220;<code>Hello you!</code>&#8221;</p>
<p>Oh no, here it comes. I can hear the cries now: &#8220;But that&#8217;s gonna cause untold problems and bugs in my code!&#8221;. Not if you use it wisely! Part of Ruby&#8217;s philosophy is to provide you with all the tools you need, then entrusting you to use that power with care. What was it Uncle Ben said to Peter? &#8220;With great power comes great responsibility.&#8221;</p>
<p>This type of feature may seem a little dodgy, but in practice it becomes extremely useful. It means that I can reopen any class that I want, at any time, which often leads to more manageable code.</p>
<p>But I&#8217;m not just limited to reopening my own classes. I can even reopen Ruby&#8217;s base classes. This is referred to as &#8220;Monkey patching&#8221;, and is usually frowned upon, as it is not very wise to be overriding the core of the language. But if used carefully and sparingly, we are able to add a little extra functionality to our app by extending a base class or two.</p>
<p>When it comes to Rails, Ruby&#8217;s base classes are extended quite a bit, especially within its ActiveSupport library, which reopens the String, Integer, and many other base classes. But it does so to add a lot of extra, and very useful functionality. It&#8217;s done so with care and attention.</p>
<p>For example, I want to know if an integer is odd or even, but Ruby provides no such method for doing so, so I write my own. But instead of writing a standalone function within my class, I can reopen Ruby&#8217;s Integer class like so:</p>
<p><code>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw1">class</span> <span class="kw3">Integer</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> odd?</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">self</span> <span class="sy0">%</span> <span class="nu0">2</span> == <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>and now I can call:</p>
<p><code>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1"><span class="nu0">3</span>.<span class="me1">odd</span>?</div>
</li>
</ol>
</div>
<p></code></p>
<p>which would of course return true. All I am doing is reopening the Integer class, and adding a new method.</p>
<p>&#8220;But what&#8217;s that question mark doing there? Is that part of the method name?&#8221; Actually, yes it is. In PHP, you can only use letters, number, and underscores in method and function names. But Ruby lets be a little more expressive, and use other characters such as <code>?</code>, <code>!</code>, and even <code>/</code>. So in the <code>odd</code> example above, I ended the method name with a question mark, because what I am doing is effectively asking a question of the number. &#8220;Are you an odd integer?&#8221;. Which it replies with a true or false value. Neat hey?</p>
<p>One of the most powerful uses of reopening classes, is the plugin system available in Rails. We can use Rails plugins to extend and override nearly every part of Rails. Which is why there are thousands of Rails plugins. You can do pretty much anything you want.</p>
<p>So I hope I have provided a good, solid reason why Ruby is better than PHP, with a feature that simply cannot be achieved with PHP. It really is very powerful, that when used carefully, can save you a lot of time, and make coding with Ruby lots of fun. Which after all, that&#8217;s why we&#8217;re here isn&#8217;t it?!</p>
<p>Next time, I think I&#8217;ll talk about Blocks; yet another feature of Ruby that PHP cannot emulate.</p>
<img src="http://feeds.feedburner.com/~r/DevelopingWithStyle/~4/SHgVYVnpt0c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://developingwithstyle.com/2009/05/21/10-reasons-why-ruby-is-better-than-php-reason-2/feed/</wfw:commentRss>
		<feedburner:origLink>http://developingwithstyle.com/2009/05/21/10-reasons-why-ruby-is-better-than-php-reason-2/</feedburner:origLink></item>
		<item>
		<title>10 Reasons why Ruby is better than PHP - Reason #1</title>
		<link>http://feedproxy.google.com/~r/DevelopingWithStyle/~3/xfDwT-anowY/</link>
		<comments>http://developingwithstyle.com/2009/05/20/10-reasons-why-ruby-is-better-than-php-reason-1/#comments</comments>
		<pubDate>Wed, 20 May 2009 09:47:53 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
		
		<category><![CDATA[Codaset]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://developingwithstyle.com/?p=350</guid>
		<description><![CDATA[So it seems that I didn&#8217;t provide the best of examples in my last post, when explaining why I chose Ruby on Rails instead of CakePHP for developing Codaset. So I decided that I will write 10 [solid] reasons, why Ruby is better than PHP. After all, that is why I chose it.
I already explained [...]]]></description>
			<content:encoded><![CDATA[<p>So it seems that I didn&#8217;t provide the best of examples in <a href="http://developingwithstyle.com/2009/05/19/why-i-chose-ruby-on-rails-instead-of-cakephp-for-codaset/">my last post</a>, when explaining <a href="http://developingwithstyle.com/2009/05/19/why-i-chose-ruby-on-rails-instead-of-cakephp-for-codaset/">why I chose Ruby on Rails instead of CakePHP</a> for developing <a href="http://codaset.com">Codaset</a>. So I decided that I will write 10 [solid] reasons, why Ruby is better than PHP. After all, that is why I chose it.</p>
<p>I already explained a little bit about the aesthetics of Ruby, and how you can write a method without the need for any curly braces or even parenthesis. So I think I will leave the aesthetics alone for a little bit.</p>
<p><strong>What a wonderful Objectified world!</strong></p>
<p>I think one of the most significant differences between Ruby and PHP, and perhaps the basis for a lot of other features of Ruby, is that everything in Ruby is an object. Even though PHP supports Object Oriented Programming, OOP was not even thought of when PHP started out. OOP was simply hacked into the language, and so is not a pure object-oriented language. That is because it uses primitive types; things that are not objects. We use primitive types all the time in PHP. Things like integers, floats and strings are all primitive types. Pretty much all we can do with them, is store data in them and pass them around. On their own, they do very little.</p>
<p>Ruby is a pure object-oriented language, and it was built like that from the start.</p>
<p>So why should I care? I hear you say. Well, because everything in Ruby is an object, everything accepts method calls, even <code>nil</code>. (<code>nil</code> is Ruby&#8217;s equivalent to PHP&#8217;s <code>null</code>.)  In PHP, a string isn&#8217;t smart enough to do anything on its own. If we want to get the length of a string, we would have to call a separate function, and pass the string to it:</p>
<div class="geshi no php">
<div class="head">PHP</div>
<ol>
<li class="li1">
<div class="de1"><span class="re1">$my_string</span> <span class="sy0">=</span> <span class="st0">&#39;Just another string&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> <span class="kw3">strlen</span><span class="br0">&#40;</span><span class="re1">$my_string</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>With Ruby, we can ask the string directly, to tell us its length:</p>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1">my_string = <span class="st0">&quot;Just another string&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">puts</span> my_string.<span class="me1">length</span></div>
</li>
</ol>
</div>
<p>FYI: PHP variables always start with a dollar sign <code>$</code>, but Ruby doesn&#8217;t have this requirement. Yet another keystroke saved. Yay! You also may have noticed that we also don&#8217;t need to type the semi-colon <code>;</code> at the end of each line in Ruby. You can, but you don&#8217;t have to, unless of course the above two lines are placed on the same line. In which case, the semi-colon will be needed to separate the two.</p>
<p>We could also write the above ruby code like this:</p>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw3">puts</span> <span class="st0">&quot;Just another string&quot;</span>.<span class="me1">length</span></div>
</li>
</ol>
</div>
<p><strong>Chicken or Egg?</strong></p>
<p>One of the biggest frustrations for me when it comes to coding in PHP, is the need to memorise the order of arguments to the language&#8217;s many global functions. I&#8217;ve been coding in PHP for over 10 years now, and I still have trouble remembering which argument comes first in a function call. Was it the chicken or was it the egg?</p>
<p>For example, let&#8217;s take the <code>in_array</code> function:</p>
<div class="geshi no php">
<div class="head">PHP</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw3">in_array</span><span class="br0">&#40;</span><span class="re1">$chicken</span><span class="sy0">,</span> <span class="re1">$egg</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>And then lets compare that with the <code>array_push</code> function, which takes its arguments in the reverse order:</p>
<div class="geshi no php">
<div class="head">PHP</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw3">array_push</span><span class="br0">&#40;</span><span class="re1">$egg</span><span class="sy0">,</span> <span class="re1">$chicken</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>Most of PHP&#8217;s array functions take an array as the first argument, but there are a few exceptions. Inconsistencies such as this can get very frustrating, and PHP is riddled with them. When you don&#8217;t know any differently, you tend to overlook these. But ever since I started working with Ruby, these little annoyances actually become big annoyances.</p>
<p>But this is the nature of PHP and it&#8217;s procedural programming design. Fortunately, Ruby solves this problem with object orientation. Lets look at Ruby&#8217;s equivalents to <code>in_array</code> and <code>array_push</code>.</p>
<p>First we create an array and assign it to the <code>fruit</code> variable:</p>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1">fruit = <span class="br0">&#91;</span><span class="st0">&#39;apple&#39;</span>, <span class="st0">&#39;orange&#39;</span><span class="br0">&#93;</span></div>
</li>
</ol>
</div>
<p>Then we check to see if <code>'banana'</code> is in the fruit array. This is the same as PHP&#8217;s <code>in_array</code> function:</p>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1">fruit.<span class="kw1">include</span>? <span class="st0">&#39;banana&#39;</span></div>
</li>
</ol>
</div>
<p>So the above obviously returns <code>false</code>, as <code>'banana'</code> does not exist, or is not included in the <code>fruit</code> array. So lets push it in:</p>
<div class="geshi no ruby">
<div class="head">RUBY</div>
<ol>
<li class="li1">
<div class="de1">fruit.<span class="me1">push</span> <span class="st0">&#39;banana&#39;</span></div>
</li>
</ol>
</div>
<p>And now <code>fruit</code> equals <code>['apple', 'orange', 'banana']</code></p>
<p>In Ruby, an array is an object, so if we want to push a variable into the array, we simply take that array and just call <code>push</code>. It needs only one argument, since the method is attached to the object on which it will operate. And we now have no source of confusion.</p>
<p>Not only does this help with remembering arguments, it also helps to organize the Ruby code space. Instead of having many procedural functions in a global space like PHP, Ruby packages all its methods nice and neatly into the objects that actually need them.</p>
<p>Of course, there are many more reasons why objectifying everything can help you, but these are some of the most notable. But I hope you can see that Ruby&#8217;s object-oriented nature actually makes life simpler.</p>
<p>In my next post where I explain reason #2 as to why Ruby is better than PHP, I will attempt to explain and show you one of the biggest differences, and the reason why developing plugins in Rails is so powerful.</p>
<p>Thanks for tuning in guys, and don&#8217;t forget to leave your comments. I&#8217;d love to know your thoughts and opinions.</p>
<img src="http://feeds.feedburner.com/~r/DevelopingWithStyle/~4/xfDwT-anowY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://developingwithstyle.com/2009/05/20/10-reasons-why-ruby-is-better-than-php-reason-1/feed/</wfw:commentRss>
		<feedburner:origLink>http://developingwithstyle.com/2009/05/20/10-reasons-why-ruby-is-better-than-php-reason-1/</feedburner:origLink></item>
		<item>
		<title>Why I chose Ruby on Rails instead of CakePHP for Codaset</title>
		<link>http://feedproxy.google.com/~r/DevelopingWithStyle/~3/uibMJ6hp270/</link>
		<comments>http://developingwithstyle.com/2009/05/19/why-i-chose-ruby-on-rails-instead-of-cakephp-for-codaset/#comments</comments>
		<pubDate>Tue, 19 May 2009 09:13:49 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[Codaset]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://developingwithstyle.com/?p=346</guid>
		<description><![CDATA[So Codaset is being written in Ruby on Rails and not CakePHP. A bold decision I think, but a decision backed up by years of sitting on the fence when it came to Ruby and Rails, but also a little controversial, since I have been working with PHP and Cake for a long time now. [...]]]></description>
			<content:encoded><![CDATA[<p>So <a href="http://codaset.com">Codaset</a> is being written in Ruby on Rails and not CakePHP. A bold decision I think, but a decision backed up by years of sitting on the fence when it came to Ruby and Rails, but also a little controversial, since I have been working with PHP and Cake for a long time now. So I wanted to lay out my reasons for going with Rails for the development of Codaset.</p>
<p>CakePHP is and probably always will be, the best PHP framework out there. I like to think that it has its head screwed on. Of course, that is all about the heads of the core team leading it, but as long as they stay focused on what Cake is there for, then it should stay like that. I continue to use Cake extensively with my day job at <a href="http://shermanstravel.com">ShermansTravel</a>, and will do so for while to come. But, ever since I discovered Ruby on Rails a few years ago, I&#8217;ve been constantly teased by it&#8217;s power - due largely in part by the Ruby language it is written in. A power that PHP simply does not have, and probably never will. Cake is severely limited by PHP, and makes the most of a language that is probably a little inadaquet at times. Unfortunately PHP has bad design decisions written all over it.</p>
<p>In fact, there is not a day goes by, that PHP does not frustrate me with its design. A task completed in PHP, is - most of the time - completed so much more elegantly if done in Ruby. Ruby is just so damn expressive, and actually, very, very natural. PHP is not! I mean, why the hell should I have to write curly braces around every single expression, or wrap things in parenthesis? In Ruby, parentheses are optional in method calls, except to clarify which parameters go to which method calls.</p>
<p>This is PHP:</p>
<div class="geshi no php">
<div class="head">class Parrot {</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> say<span class="br0">&#40;</span><span class="re1">$word</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// say it here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>And this is Ruby:</p>
<div class="geshi no ruby">
<div class="head">class Parrot</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> say<span class="br0">&#40;</span>word<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1"># say it here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
</ol>
</div>
<p>Now isn&#8217;t that so much nicer?</p>
<p>Yes, I know that&#8217;s all about aesthetics, but I like my code to look good. It makes looking at it and working with it, much more pleasant. But also this is just one of the hundreds of reasons why I chose Ruby and Rails. I suppose you could name this post &#8220;Why I chose Rails instead of Cake: <strong>Reason #1</strong>&#8220;.</p>
<p>The bottom line, is that I have played around with Rails for years now, but never took the plunge and wrote an entire - completed - application with it. I started a few times, but then went back to using Cake. But this was because I didn&#8217;t know Ruby well enough, and because of that, it was taking a long time to do something that I could have done much quicker in PHP. But because of all that tinkering, I can confidently say that I now know the basics of Ruby and can quite easily write a Ruby script, or a full Rails app.</p>
<p>So writing Codaset in Rails was the right decision. Every time I open it up in Textmate, I&#8217;m excited by what I see. Everytime I see another Rails plugin or Ruby gem, I want to install it. The thought of developing the next feature of Codaset exites me in ways that PHP almost never has. And because of all that, I&#8217;m learning a new programming language at an incredible rate. Once you get past the learning curve, you really start zooming through any app.</p>
<p>I&#8217;m even considering open sourcing Codaset! I want the world to see what I&#8217;ve done, and how I did it.</p>
<p>Lovin&#8217; it!</p>
<img src="http://feeds.feedburner.com/~r/DevelopingWithStyle/~4/uibMJ6hp270" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://developingwithstyle.com/2009/05/19/why-i-chose-ruby-on-rails-instead-of-cakephp-for-codaset/feed/</wfw:commentRss>
		<feedburner:origLink>http://developingwithstyle.com/2009/05/19/why-i-chose-ruby-on-rails-instead-of-cakephp-for-codaset/</feedburner:origLink></item>
		<item>
		<title>So what the hell is Codaset?</title>
		<link>http://feedproxy.google.com/~r/DevelopingWithStyle/~3/tkAd1ypaRVM/</link>
		<comments>http://developingwithstyle.com/2009/05/13/so-what-the-hell-is-codaset/#comments</comments>
		<pubDate>Wed, 13 May 2009 16:28:24 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
		
		<category><![CDATA[Web Dev]]></category>

		<guid isPermaLink="false">http://developingwithstyle.com/?p=333</guid>
		<description><![CDATA[Ever since I began working for ShermansTravel, and in particular, since I became more involved with CakePHP and open source software, I&#8217;ve spent a LOT of time using Trac and Github. Trac is somewhat of a defacto standard for managing software projects, and is what we use at work. Github is what I use for [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://codaset.com"><img class="alignleft size-full wp-image-343" title="Codaset Logo" src="http://developingwithstyle.com/wp-content/uploads/2009/05/00000002_r4_c2.jpg" alt="Codaset Logo" width="181" height="53" /></a>Ever since I began working for <a href="http://www.shermanstravel.com">ShermansTravel</a>, and in particular, since I became more involved with <a href="http://cakephp.org">CakePHP</a> and open source software, I&#8217;ve spent a LOT of time using <a href="http://trac.edgewall.org/">Trac</a> and <a href="http://github.com/joelmoss">Github</a>. Trac is somewhat of a defacto standard for managing software projects, and is what we use at work. Github is what I use for my personal and open source projects; and is also what I wish I could use at work. Both are great at what they do, but in most cases, each does something better than the other.</p>
<p>Trac has a kickass issue tracking system, which is very customisable, and integrates very neatly with Subversion. Unfortunately it&#8217;s not especially user friendly, and doesn&#8217;t support Git. (there is a git plugin, but it&#8217;s alpha code). It&#8217;s also self hosted and written in Python, which means it&#8217;s not the easiest to get up and running.</p>
<p>Github is so hot right now! And for good reason too. It&#8217;s built entirely around the excellent Git, and implements it&#8217;s features all around the ideal&#8217;s of Git. But that&#8217;s about all it does. Only up until recently has it been given an issue tracking system, and to be honest&#8230; that sucks!</p>
<p>So basically, both Trac and Github have their strengths and their weaknesses. But each is missing a part or parts of the other.</p>
<p>So that is why I am creating <a href="http://codaset.com">Codaset</a>. Codaset aims to be all that Trac and Github are good at, in one nice, neat package. It will be based on Git, allowing it to offer great social features, but it will also have a kickass issue tracking</p>
<p>system that will integrate perfectly with your source control.</p>
<p>But I don&#8217;t intend to make this a Github/Trac clone. It&#8217;s gonna be sooo much more&#8230; eventually! Things that you cannot find on any other web based software project management system.</p>
<p><a href="http://developingwithstyle.com/wp-content/uploads/2009/05/picture-2.png"><img class="size-medium wp-image-335 alignleft" title="Git Tree screenshot" src="http://developingwithstyle.com/wp-content/uploads/2009/05/picture-2-300x229.png" alt="Git Tree screenshot" width="300" height="229" /></a></p>
<p>So this was just a little teaser, and an <a href="http://codaset.com">invite</a> for you all to <a href="http://codaset.com">join in the private beta</a>, which I hope to start soon. Take a look over at <a href="http://codaset.com">Codaset.com</a>, and signup now. I&#8217;ll be posting more about what to expect as and when I can. In the meantime, here&#8217;s a quick screen shot to wet your appetite.</p>
<p>Oh and don&#8217;t forget to <a href="http://twitter.com/joelmoss">follow me on Twitter</a>.</p>
<img src="http://feeds.feedburner.com/~r/DevelopingWithStyle/~4/tkAd1ypaRVM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://developingwithstyle.com/2009/05/13/so-what-the-hell-is-codaset/feed/</wfw:commentRss>
		<feedburner:origLink>http://developingwithstyle.com/2009/05/13/so-what-the-hell-is-codaset/</feedburner:origLink></item>
		<item>
		<title>Enhanced Controller Callbacks for CakePHP</title>
		<link>http://feedproxy.google.com/~r/DevelopingWithStyle/~3/PqpIc9A8o_c/</link>
		<comments>http://developingwithstyle.com/2009/03/23/enhanced-controller-callbacks-for-cakephp/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 19:40:26 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://developingwithstyle.com/?p=326</guid>
		<description><![CDATA[I had this idea a while ago, and actually implemented it into a Cake project, but it was a bit hacky and required hacking the core. So I didn&#8217;t like it. But today, I had a need for this same functionality, so I had a little think about how I could implement it without hacking [...]]]></description>
			<content:encoded><![CDATA[<p>I had this idea a while ago, and actually implemented it into a Cake project, but it was a bit hacky and required hacking the core. So I didn&#8217;t like it. But today, I had a need for this same functionality, so I had a little think about how I could implement it without hacking the core, and thus was born the <a href="http://github.com/joelmoss/cakephp-callback">Callback plugin</a> for CakePHP.</p>
<p>If any of you have ever used Ruby on Rails, you should be familiar with its callback system. Well, it was this system that I needed.  I just started fleshing out a new Cake app, and needed to run a <em>beforeFilter</em> callback on a couple of specific controller actions. Of course, I could do this:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> MyController <span class="kw2">extends</span> AppController <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">function</span> beforeFilter<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">params</span><span class="br0">&#91;</span><span class="st0">&#39;action&#39;</span><span class="br0">&#93;</span> <span class="sy0">==</span> <span class="st0">&#39;index&#39;</span> <span class="sy0">||</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">params</span><span class="br0">&#91;</span><span class="st0">&#39;action&#39;</span><span class="br0">&#93;</span> <span class="sy0">==</span> <span class="st0">&#39;view&#39;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co2"># do something here that only the &#39;index&#39; and &#39;view&#39; actions need</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>But that&#8217;s just not very elegant, and will get pretty messy the more you do it. So I came up with the Callback plugin, which is basically a simple component that provides some sweetly fine grained control over your callbacks. So now I can do this instead:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">class</span> MyController <span class="kw2">extends</span> AppController <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">var</span> <span class="re1">$beforeFilter</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;myCallback&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">function</span> _myCallback<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co2"># do something here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>and I can even call several callbacks, each with their own methods:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">class</span> MyController <span class="kw2">extends</span> AppController <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">var</span> <span class="re1">$beforeFilter</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;myCallback&#39;</span><span class="sy0">,</span> <span class="st0">&#39;anotherCallback&#39;</span><span class="sy0">,</span> <span class="st0">&#39;andAnotherOne&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">function</span> _myCallback<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co2"># do something here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">function</span> _anotherCallback<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co2"># do something here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">function</span> _andAnotherOne<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co2"># do something here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Now the above is lovely and all, but it doesn&#8217;t really provide any extra functionality above what Cake already provides. So far, my code just looks cleaner. What I really want to do is only run the <em>anotherCallback</em> when calling the <em>index</em> action. So I change the $beforeFilter param:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">class</span> MyController <span class="kw2">extends</span> AppController <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">var</span> <span class="re1">$beforeFilter</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;myCallback&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;anotherCallback&#39;</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;only&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;index&#39;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;andAnotherOne&#39;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>Much better! I can also specify to run a callback on all actions, except the &#8216;delete&#8217; action:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">class</span> MyController <span class="kw2">extends</span> AppController <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">var</span> <span class="re1">$beforeFilter</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;anotherCallback&#39;</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;except&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;delete&#39;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>Even better! But still I wanted more!</p>
<p>I wanted to be able to add even more control, and only run a callback if a certain condition is/not met:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">class</span> MyController <span class="kw2">extends</span> AppController <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">var</span> <span class="re1">$beforeFilter</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;anotherCallback&#39;</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;if&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;ifTrue&#39;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">function</span> _ifTrue<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co2"># do something here and return true for the callback to run</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>So the <em>anotherCallback</em> callback will now only run if the <em>_ifTrue</em> method returns true. Lovely!</p>
<p>Right now it supports the three main controller callbacks: beforeFilter, beforeRender and afterFilter. The plan is to provide the same functionality to Models.</p>
<p>You can checkout the code from Github repo at <a href="http://github.com/joelmoss/cakephp-callback">http://github.com/joelmoss/cakephp-callback</a>. Feel free to fork away!</p>
<img src="http://feeds.feedburner.com/~r/DevelopingWithStyle/~4/PqpIc9A8o_c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://developingwithstyle.com/2009/03/23/enhanced-controller-callbacks-for-cakephp/feed/</wfw:commentRss>
		<feedburner:origLink>http://developingwithstyle.com/2009/03/23/enhanced-controller-callbacks-for-cakephp/</feedburner:origLink></item>
		<item>
		<title>Cake Nibbles #4</title>
		<link>http://feedproxy.google.com/~r/DevelopingWithStyle/~3/TJA9KWu4yoI/</link>
		<comments>http://developingwithstyle.com/2009/02/09/cake-nibbles-4/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 09:10:35 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://developingwithstyle.com/?p=318</guid>
		<description><![CDATA[It&#8217;s been too long, but here&#8217;s another handful of nibbles for your CakePHP applications and development.
Plugin Server
After some great improved support for re-usable plugins in Cake 1.2, and Gwoo&#8217;s announcement mention at last years CakeFest in Florida; we are now starting to see some evidence of a fabled plugin server. The idea, is that after [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been too long, but here&#8217;s another handful of nibbles for your CakePHP applications and development.</p>
<p><strong>Plugin Server</strong></p>
<p>After some great improved support for re-usable plugins in Cake 1.2, and Gwoo&#8217;s <span style="text-decoration: line-through;">announcement</span> mention at last years CakeFest in Florida; we are now starting to see some evidence of a fabled plugin server. The idea, is that after installing a small Cake shell in your vendors directory, you would be able to install a selection of official and unoffical Cake plugins into your application via the command line. So no need to manually download, unzip and copy files into your plugins directory, as the shell would do it for you. The actual server code is also planning on becoming publicly available, so that we can all host our own CakePHP plugin server.</p>
<p>You can test the first release at <a href="http://plugins.thoughtglade.com">http://plugins.thoughtglade.com</a></p>
<p>I gotta say, that I love this idea, and would really help in spreading the word. Not to mention, make it easier to use Cake and its increasing selection of plugins. I just wish that there would be a similar way to install Cake. There are some great things to be said about systems such as Pear, which would allow you to install the Cake core in a central place on your machine. Then create apps anywhere else on the same machine.</p>
<p><strong>CakePHP 1.2.1</strong></p>
<p>Hot on the heals on the final release of CakePHP 1.2, comes a bug fix and security release. 1.2.1 includes a few bug fixes, but most importantly, fixes a nasty security risk in 1.2. So please <a href="http://cakephp.org/">upgrade</a> as soon as you can.</p>
<p><strong>API Viewer</strong></p>
<p>Up until now, the Cake API left a lot to be desired. But after some frustrations with Doxygen, Core developer Mark Story led the charge to create a Cake based API generator. And man did he do a great job. Check out the new API viewer now at <a href="http://api.cakephp.org">http://api.cakephp.org</a>, or check out and use the code at <a href="http://thechaw.com/api_generator">http://thechaw.com/api_generator</a></p>
<p><strong>DebugKit Moves home</strong></p>
<p>And just a quick reminder that the excellent DebugKit plugin has moved to a new home at The Chaw: <a href="http://thechaw.com/debug_kit">http://thechaw.com/debug_kit</a> - Don&#8217;t leave home without it!</p>
<p><strong>CakePHP Twitter Timeline</strong></p>
<p>This just in, you can now see all the buzz and twitterrings about Cake in a very pleasing way: <a href="http://cakealot.com/cakephp-timeline.html">http://cakealot.com/cakephp-timeline.html</a></p>
<p><strong>BedPosted</strong></p>
<p>And this is just great. A web app which you can use to keep track of your sex life. Haven&#8217;t you always wondered how often you scored this past month, or would like to see a rolling sex history of your life? Well worry no more, coz some bright spark has done just that, and it&#8217;s all done with CakePHP. Check out <a href="http://www.bedposted.com/">BedPost</a>.</p>
<img src="http://feeds.feedburner.com/~r/DevelopingWithStyle/~4/TJA9KWu4yoI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://developingwithstyle.com/2009/02/09/cake-nibbles-4/feed/</wfw:commentRss>
		<feedburner:origLink>http://developingwithstyle.com/2009/02/09/cake-nibbles-4/</feedburner:origLink></item>
		<item>
		<title>And finally… CakePHP 1.2 is stable and final!</title>
		<link>http://feedproxy.google.com/~r/DevelopingWithStyle/~3/_meqGjCnZik/</link>
		<comments>http://developingwithstyle.com/2008/12/27/and-finally-cakephp-12-is-stable-and-final/#comments</comments>
		<pubDate>Sat, 27 Dec 2008 14:30:12 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
		
		<category><![CDATA[Ramblings]]></category>

		<guid isPermaLink="false">http://developingwithstyle.com/?p=315</guid>
		<description><![CDATA[Just a quick one, as I am sure that you have already read, but I am extremely pleased to tell you all that CakePHP has been released as a final and stable release. This means no more alpha&#8217;s, beta&#8217;s or release candidates. Get it while it is hot, as it is very HOT!
Big congrats go [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick one, as I am sure that you have already <a href="http://bakery.cakephp.org/articles/view/the-gift-of-1-2-final">read</a>, but I am extremely pleased to tell you all that CakePHP has been released as a final and stable release. This means no more alpha&#8217;s, beta&#8217;s or release candidates. <a href="http://cakephp.org">Get it while it is hot</a>, as it is very HOT!</p>
<p>Big congrats go to Nate, Gwoo and all the core team. Well done guys! On to 1.3&#8230;</p>
<img src="http://feeds.feedburner.com/~r/DevelopingWithStyle/~4/_meqGjCnZik" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://developingwithstyle.com/2008/12/27/and-finally-cakephp-12-is-stable-and-final/feed/</wfw:commentRss>
		<feedburner:origLink>http://developingwithstyle.com/2008/12/27/and-finally-cakephp-12-is-stable-and-final/</feedburner:origLink></item>
	</channel>
</rss>
