<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP in Action &#187; Ruby</title>
	<atom:link href="http://blog.agilephp.com/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.agilephp.com</link>
	<description>Dagfinn Reiersøl on PHP, agile development, Ruby and other addictive substances</description>
	<lastBuildDate>Mon, 28 Sep 2009 14:35:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Real programming with PHP 5.3 (part 1): array processing</title>
		<link>http://blog.agilephp.com/2009/03/19/real-programming-with-php-53-part-1-array-processing/</link>
		<comments>http://blog.agilephp.com/2009/03/19/real-programming-with-php-53-part-1-array-processing/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 09:48:33 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.agilephp.com/?p=1513</guid>
		<description><![CDATA[You may have heard of the new features that are scheduled for PHP 5.3, but who knows what they can be used for in real programming? I took the time for some experimental research, and came up with a few examples.
At the PHP UK conference, I saw Sebastian Bergmann&#8217;s presentation on lambda functions, closures and [...]]]></description>
			<content:encoded><![CDATA[<p>You may have heard of the new features that are scheduled for PHP 5.3, but who knows what they can be used for in real programming? I took the time for some experimental research, and came up with a few examples.</p>
<p>At the PHP UK conference, I saw <a href="http://www.slideshare.net/sebastian_bergmann/of-lambda-functions-closures-and-traits">Sebastian Bergmann&#8217;s presentation on lambda functions, closures and traits</a>. Lambda functions and closures are scheduled for 5.3  (or fivethree, as I&#8217;ve affectionately named the binary), or later. The presentation was intriguing, but has only simplistic examples, sufficient to illustrate the mechanical aspects. It left me curious as to what these features could be used for in real programs.</p>
<p>Before we start playing, let&#8217;s get the basic terminology clear. Lambda functions and closures are two fancy names for (variations on) anonymous functions. These are functions that are more flexible than ordinary functions and can be stored in variables or defined just where they are needed.</p>
<p>Anonymous functions are possible even in pre-5.3 PHP. They can be created using <code>create_function()</code>. But this is cumbersome, ugly, error-prone and possibly insecure. PHP 5.3 lets you do this in a much more natural and pleasant way.</p>
<p>The most basic and obvious case is sorting. Here is an example from Sebastian&#8217;s presentation:</p>
<pre class="brush: php">

&lt;?php

usort(
    $list,
    function ($a, $b) {
        if ($a == $b) return 0;
        return ($a &lt; $b) ? -1 : 1;
    }
);
</pre>
<p>In PHP before 5.3, this must be done either by using <code>create_function()</code> or by defining the function somewhere else, away from where it&#8217;s actually used. The above is clearly much better.</p>
<p>What else can we do with anonymous functions? In Ruby, they are the standard way of processing arrays. For example, if we want to process a set of files, adding a directory name at the beginning, we can do this:</p>
<pre class="brush: ruby">
files = [&#039;pdo.a&#039;,&#039;pdo.la&#039;,&#039;pdo.so&#039;,&#039;sqlite.a&#039;,&#039;sqlite.la&#039;,&#039;sqlite.so&#039;];
files_with_dir = files. map  { |f| &quot;modules/#{f}&quot; }
</pre>
<p>The snippet inside the braces is a function. <code>map</code> is the Ruby equivalent of PHP&#8217;s <code>array_map()</code>, except that Ruby is built to do this particular trick. But let&#8217;s try the equivalent with PHP 5.3. First we define the array:</p>
<pre class="brush: php">
$files = array(&#039;pdo.a&#039;,&#039;pdo.la&#039;,&#039;pdo.so&#039;,&#039;sqlite.a&#039;,&#039;sqlite.la&#039;,&#039;sqlite.so&#039;);
</pre>
<p>And then we process it:</p>
<pre class="brush: php">
$filesWithDir = array_map(
    function($f) { return &quot;modules/$f&quot; ; },
    $files
);
</pre>
<p>Interesting, but not necessarily better than the conventional alternative:</p>
<pre class="brush: php">
$filesWithDir = array();
foreach ($files as $file) {
    $filesWithDir[] = &quot;modules/$file&quot;;
}
</pre>
<p>Using array_map() seems like a realistic option, at least for some cases. It reads differently because it&#8217;s conceptually different, focusing on processing the entire array rather than one element at a time. You might find it cleaner. And in some cases, it will be clearly superior. If you have a slightly more complex structure than a simple one-dimensional array, you can use <code>array_map_recursive()</code> to simplify the code dramatically.</p>
<p>In <a href="http://blog.agilephp.com/2009/03/31/real-programming-with-php-53-part-2-javascript-style-classes/">part 2</a>, I&#8217;ll present an example of a JavaScript-style class composed of closures.</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/fcdb80d8-75b2-45a3-8655-b9e16459cab8/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_a.png?x-id=fcdb80d8-75b2-45a3-8655-b9e16459cab8" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=Real%20programming%20with%20PHP%205.3%20%28part%201%29%3A%20array%20processing&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2009%2F03%2F19%2Freal-programming-with-php-53-part-1-array-processing%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2009/03/19/real-programming-with-php-53-part-1-array-processing/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Refactoring is design</title>
		<link>http://blog.agilephp.com/2008/10/11/refactoring-is-design/</link>
		<comments>http://blog.agilephp.com/2008/10/11/refactoring-is-design/#comments</comments>
		<pubDate>Sat, 11 Oct 2008 23:41:24 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Application design]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Smidig2008]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=1406</guid>
		<description><![CDATA[ Refactoring is by definition a design actitivity, since the definition of refactoring is &#8220;improving the design of existing code&#8221;. But is this generally and fully recognized? After attending my friendly local agile conference (Smidig2008&#8212;sorry, it&#8217;s in Norwegian), I&#8217;m getting more of a feel for how different people think about it. And I&#8217;m wondering whether [...]]]></description>
			<content:encoded><![CDATA[<p> Refactoring is by definition a design actitivity, since the definition of refactoring is &#8220;improving the design of existing code&#8221;. But is this generally and fully recognized? After attending my friendly local agile conference (<a href="http://smidig.no/smidig2008/">Smidig2008</a>&mdash;sorry, it&#8217;s in Norwegian), I&#8217;m getting more of a feel for how different people think about it. And I&#8217;m wondering whether the use of metaphors such as &#8220;cleaning&#8221; makes refactoring seem too much like unskilled labor.  After all, physical cleaning jobs are seen that way. </p>
<p> The analogy between cleaning and refactoring is useful for making the non-developers understand that refactoring is absolutely necessary. But beyond this pragmatic similarity, are the two really similar in deep and meaningful ways? I don&#8217;t think so. Refactoring is not unskilled labor. It&#8217;s a task that both requires and builds design skill and experience.  While anyone can see that a floor is dirty, identifying <a href="http://en.wikipedia.org/wiki/Code_smell">code smells</a> is non-obvious, tricky and demanding.  This is true even of the simplest code smell, duplicated code. Although spotting code duplication is sometimes easy, at other times, the duplication is too subtle to be easily identifable.  When you clean a floor, the goal is well-defined and easy to visualize. When refactoring, you may know what you&#8217;re aiming for at each small step, but just a few moves further ahead you may end up with a structure you hadn&#8217;t imagined. </p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=Refactoring%20is%20design&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2008%2F10%2F11%2Frefactoring-is-design%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2008/10/11/refactoring-is-design/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Immediacy and frameworks</title>
		<link>http://blog.agilephp.com/2008/04/06/immediacy-and-frameworks/</link>
		<comments>http://blog.agilephp.com/2008/04/06/immediacy-and-frameworks/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 16:21:22 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Application design]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=1357</guid>
		<description><![CDATA[As if to remind us that not everyone in Ruby hates PHP and vice versa, Rails creator David Heinemeier Hansson commends the immediacy of PHP.
I&#39;m vaguely wondering if he&#39;s also understood the potential of the smooth, agile ride from the immediate to the complex. Probably not.
To quote myself from the Sitepoint PHP Application Design forum, [...]]]></description>
			<content:encoded><![CDATA[<p>As if to remind us that not everyone in Ruby hates PHP and vice versa, Rails creator David Heinemeier Hansson commends <a href="http://www.loudthinking.com/posts/23-the-immediacy-of-php">the immediacy of PHP</a>.</p>
<p>I&#39;m vaguely wondering if he&#39;s also understood the potential of the smooth, agile ride from the immediate to the complex. Probably not.</p>
<p>To quote myself from the Sitepoint PHP Application Design forum, to the question <a href="http://www.sitepoint.com/forums/showthread.php?t=541306">When do you think it is time to develop a custom framework?</a>, I answered:<br />
<blockquote>Never. Always. Start dirt simple and refactor to eliminate duplication. When you need a specific feature, study how it&#39;s done in existing frameworks and implement it. That will get you a &#8220;framework&#8221; that has exactly what you need.</p></blockquote>
<p>This is an approach that is facilitated by the immediacy of PHP. Is this approach slower, long-term, than adopting a framework? That probably depends on a lot of variables. If an existing framework does everything your application<br />needs, it may be a great idea to use it. On the other hand, if you need to do things that are significantly different from what the framework supports, you need to start mucking about with the framework itself. In that case, it may be more<br />effective to have you own &#8220;framework&#8221; that fits the application like a glove, without a lot of features you will never need, and without features that are almost, but not quite what you need.</p>
<p>I love Ruby, but I&#39;ve never actually tried Rails. Reading Rails tutorials, I&#39;m not quite as impressed as some folks I know.</p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=Immediacy%20and%20frameworks&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2008%2F04%2F06%2Fimmediacy-and-frameworks%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2008/04/06/immediacy-and-frameworks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Paparrazzi testing</title>
		<link>http://blog.agilephp.com/2007/10/09/paparrazzi-testing/</link>
		<comments>http://blog.agilephp.com/2007/10/09/paparrazzi-testing/#comments</comments>
		<pubDate>Tue, 09 Oct 2007 11:15:19 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=1347</guid>
		<description><![CDATA[
 	
Uncle Bob (Robert C. Martin) has blogged about testing GUIs (web interfaces, that is; JSP and Rails) without having to run a web server. I don&#39;t think it answers all the challenges I&#39;m up against currently, but it inspired me to some heavy thinking about the essential issue of complete and not too cumbersome [...]]]></description>
			<content:encoded><![CDATA[<p><meta content="text/html; charset=utf-8" http-equiv="CONTENT-TYPE" /><title></title><meta content="OpenOffice.org 2.2  (Linux)" name="GENERATOR" /><meta content="16010101;0" name="CREATED" /><meta content="16010101;0" name="CHANGED" /><br />
<style type="text/css"> 	</style>
<p>Uncle Bob (Robert C. Martin) has blogged about <a href="http://blog.objectmentor.com/articles/category/testing-guis">testing GUIs</a> (web interfaces, that is; JSP and Rails) without having to run a web server. I don&#39;t think it answers all the challenges I&#39;m up against currently, but it inspired me to some heavy thinking about the essential issue of complete and not too cumbersome test covrerage
<p style="margin-bottom: 0cm">My conclusion so far is that Uncle Bob&#39;s articles is an example of what we may call the paparazzi principle.</p>
<p style="margin-bottom: 0cm">Paparazzi try get as close as possible to celebrities they prey upon. The fewer walls or windows between the camera and the victim, the better. Pictures should be as revealing as possible; naked is or half-naked is good.</p>
<p style="margin-bottom: 0cm">The paparrazzi principle for web testing is this: run the test from &ldquo;above&rdquo; as close as possible to the component you want to test. Inject mock objects from &ldquo;below&rdquo; as close as possible to the component you want to test. Even in ordinary unit testing, this can be far from trivial. Where the web interface itself is involved, it is much harder.</p>
<p style="margin-bottom: 0cm">It follows that you don&#39;t want to use a real web browser or web server unless you must. It also follows that a realtively simplistic web client (SimpleTest&#39;s web browser is a good example) without a user interface is preferable to a web browser for human use. Unless you absolutely need it, that is. Therefore, Selenium is probably a step in the wrong direction. I want test tools that enable me to test user interaction without having to fire up these voluminous masses of software.</p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=Paparrazzi%20testing&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2007%2F10%2F09%2Fpaparrazzi-testing%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2007/10/09/paparrazzi-testing/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
