
Originally Posted by
samsm
Would I be incorrect in saying that what is referred to in PHP as design patterns is called "the Ruby Way" in Ruby?
Yes, it would be incorrect. I think "the Ruby Way" would be best summed up as "do everything in such a way that it looks so stupidly simple that you wouldn't want to do it any other way."
I think Try Ruby is a good example of "The Ruby Way". Simple, looks good, works well.
"Design patterns" in PHP means "Patterns are ways to describe best practices and good designs. They show a flexible solution to common programming problems." with code that looks like this:
PHP Code:
<?php
class Example
{
// Hold an instance of the class
private static $instance;
// A private constructor; prevents direct creation of object
private function __construct()
{
echo 'I am constructed';
}
// The singleton method
public static function singleton()
{
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
// Example method
public function bark()
{
echo 'Woof!';
}
// Prevent users to clone the instance
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}
?>
"The Ruby Way" says patterns shouldn't be put in the documentation to be copy-pasted all over the place. It says copy-pasting isn't fun, and it doesn't look simple: it just looks ugly. "The Ruby Way" says code should be beautiful.
With the Ruby Way, you'd just have this:
Code:
class Example
include Singleton
end
It looks stupidly simple, why would you want to write anything more?
Douglas
Bookmarks