Posts tagged as:

good parts

Welcome to Ruby The Good Parts. This blog is part of the HackingtheValley.com empire of Good Parts blogs for PHP, Ruby, and Smalltalk. As we create Ruby content, we will post it here.

In the meantime, check out Hacking The Valley and PHP The Good Parts.

TTYL,

Douglas Putnam

0 comments

I’m lazy when it comes to writing valid HTML. I know it’s the right thing to do, but I’ve developed some bad habits over the years. There are too many times when I forget quotes, alt tags, and ending tags. So I’ve decided to outsource my HTML generation to a machine: Textile.

Textile is a “human web text generator”. I like Textile because I can use simple formatting cues to generate any HTML you want. A simple example would be,

p. This is a paragraph. "A link to a URL":http://someurl

Textile will turn that text into this HTML:

<p>This is a paragraph. <a href="http://someurl">A link to a <spanclass="caps">URL</span></a></p>

In my CCSF courses I’ve developed a small PHP framework that I use as a teaching tool for my advanced PHP course. I call it MojoMVC. MojoMVC generates output of any kind by employing Decorator classes to format the output. The default format is HTML, but Decorators can be of any kind, including XML, PDF, images, and so on. All you have to do is write the class for the format you want. I created a HTMLDecorator class to have Textile ease my HTML woes.

MojoMVC Overview:

  1. MojoMVC is a conventional MVC framework that receives a request for the URL http://you.com/cs130a/syllabus.
  2. Controller classes are mapped to URLs.
  3. Which means, there is a cs130aController class and a syllabus page to be displayed.
  4. Every controller class has a render method defined in the parent class, ApplicationController.
  5. Every render method uses a Decorator class to format output.

The render in ApplicationController looks like this:

 1  class ApplicationController extends Base {
 2      // snip //
 3      function render() {
 4         // Default is HTMLDecorator
 5         $klass = $this->as.'Decorator';
 6         // Decorators all have a render()
 7         $decorator = new $klass;
 8         // Pass the current instance to the decorator for rendering.
 9         $decorator->render($this) ;
10         exit;
11      }
12  } 

By the way, one of PHP’s Good Parts in visible in this method—the easy implementation of polymorphism in lines 5, 7, and 9.

After we define the render method, the next step is to create a Decorator that will take the current instance and render its output using Textile. Here’s how it works.

The MojoMVC HTMLDecorator

 1 class HTMLDecorator
 2 {
 3
 4     function render($klass)
 5     {   // $klass is instance of current controller
 6         $textile = new Textile; // new Textile object
 7         // Check to make sure we have the requested object
 8         if (file_exists(VIEWS.$klass->r->controller.'/'.$klass->r->action.'.html'))
 9         {
10             // If so, run the method requested
11             if (method_exists($klass->r->controller.'Controller', $klass->r->action))
12             {
13                 $action = $klass->r->action;
14                 $klass->$action();
15             }
16             // Buffer output so we can pass it to Textile
17             ob_start();
18             include (VIEWS.$klass->r->controller.'/'.$klass->r->action.'.html');
19             // Textileize the HTML
20             $klass->out = $textile->textileThis(ob_get_clean());
21         } else
22         {
23             include (VIEWS.$klass->r->controller.'/'.$klass->r->error404.'.html');
24         }
25         // Include the layout HTML file that displays the requested page.
26         if (file_exists(LAYOUTS.$klass->r->controller.'.html'))
27         {
28             include LAYOUTS.$klass->r->controller.'.html';
29         } elseif (file_exists(LAYOUTS.'default.html'))
30         {
31             include LAYOUTS.'default.html';
32         } else
33         {
34             print $klass->out;
35         }
36     }
37 }

Most of this code is all about error checking. Textileizing is a matter of saving the output (using ob_start and ob_get_clean), loading Textile, creating an instance, and running the text through the textileThis method.

To learn more about Textile, check out the Textile home page. Textile has been ported to Ruby, where it is known as RedCloth.

For an easy-to-digest introduction to the Decorator design pattern, check out the Head First Design Patterns sample chapter.

Happy hacking…

0 comments

I found Dynamic Web Development with Seaside and made a small breakthrough. I’m finding that learning Smalltalk is harder than I had anticipated. I’m accustomed to learning languages quickly. But most of the languages I’ve learned are C-based: C, C++, Java, PHP, Perl, etc. Even Ruby is in that club.

But Smalltalk is weird and foreign to my C-biased eyes. In the beginning I was like one of those poor guys who’s lost a limb and experiences phantom sensations where his arms and legs used to be. They parts are gone, but the brain still thinks they’re out there. In my case, I’d lost my editor (vi) and the crowd of templates, source code and HTML files I’m comfortable with. I’d also lost my command line debugging tools and grep, not to mention that cozy Rails directory structure I’ve been using for the last 4 years. I felt that suddenly I was working in the dark, without eyes, without hands.

Part of the problem is that I am impatient. I want to see some quick results, as I did when I learned Ruby. I want to get my test site (the e-card site I always build with a new language) up and running yesterday. But I there I was flailing and failing at the thought of opening a directory and reading a list of files.

Fortunately I don’t give up if there’s the faintest glimmer of hope. In my case, that glimmer came when I was reading through Mark Guzdial’s Squeak: Object-oriented design with multimedia applications and found that I could get a directory handle very simply with this line:

directory = FileDirectory on: pathToImages

and an array of file names with these lines:

imageNames := OrderedCollection new.
directory fileNames
do: [ :p | imageNames add: p ]

What relief I felt when I wrote that code and it worked. I was back on familiar turf, with a solid array of file names to work with.

To paraphrase Archimedes, If you give me a array and a place to stand, I can move the world. My world, at least.

4 comments

This paper gives a crystal-clear description of Extreme Programming (XP). I found it especially interesting in that it describes how to use Squeak in an XP environment.

Embracing Change with Squeak: Extreme Programming (XP)

0 comments

My favorite way to learn a new programming language is to build a project with the new language. I learn best when I’m having fun, and I test my new languages by building a simple database-backed e-card web site.

E-card sites are old hat, but they cover a lot of territory: you learn to read directories, manipulate images, create HTML, process forms, store information in a database, and send e-mail. I like this project so much that I use it in my PHP and Ruby classes at CCSF. Here’s the kind of site I end up with. I’m not a designer, so I get some slack on the way the site looks.

Since there are several popular Smalltalk dialects, I have to make an important decision. The problem is that I’m a newbie, so I’m going to have to guess which direction to go: commercial or open source.

I know that Cincom is a leading provider of Smalltalk solutions. The Cincom Smalltalk evangelist (James Robertson) provides a steady stream of good content, commentary, and instructional videos on cutting edge topics, such as WebVelocity and Seaside, my ultimate destination. Cincom VisualWorks is available as a free non-commercial community edition.

VisualWorks on my Hackintosh

My other choice is Squeak, an open source Smalltalk. Alan Kay, the creator of Smalltalk, is part of this project. A few years ago I worked my through Learn Programming With Robots by Stephane Ducasse. This book is designed to use Squeak to teach programming to children, and I, a rather large child, loved it. The look and feel of Squeak is rather toy-like, colorful and fun—quirky, some say. Under the surface it’s all business. The main appeal of Squeak is that I am familiar it.

However, I am leaning toward VisualWorks, but there one deterrent. I’m running a Hackintosh as my main desktop, and some VisualWorks Aqua features are not rendering correctly. The X11 version works, though it is, as you would expect, homely. I know that the real solution is to ditch the Hack and buy Genuine Apple hardware.

The Squeak interface, on the other hand, renders fine on the Hackintosh. Both VisualWorks and Squeak work perfectly on my Macbook Pro. The question is, can I live with the X11 version of VisualWorks on my work station?

Squeak on my Hackintosh

I’ll sleep on it and decide tomorrow. Will I go right brain, or left brain? I’m counting on the Collective Unconscious to help me decide.

3 comments

Have I mentioned that my latest enthusiasm is Smalltalk? It’s a good fit for what I want to do with web development, and Seaside is the application platform that dots the i‘s on all of the requirements on my web app wish list:

  • Easy statefullness
  • Introspection
  • Live image
  • Fast
  • No HTML
  • No templates

There are quite a few fine Smalltalk distributions, but I’m familiar with these: Cincom VisualWorks and Squeak Smalltalk. Since Squeak is free and there are many Squeak resources, including Squeak: Learn Programming with Robots, I’m dipped my toe into the water at Squeak.org. Squeak runs on OS X, Windows, and Linux, and it’s free.

Squeak is a fun way to program graphics without heartache and headache. Even a comparative lit major like me can do cool things..

The first time you work with Smalltalk, you may find yourself feeling like a stranger in a strange land. You don’t work with source files; you work with an image, which is like a living piece of code. You can change it while it’s running. Very cool stuff. Cooler than Ruby, to my mind, and I love Ruby.

When I first saw a demo of Seaside, I felt a shock of recognition: Seaside is the framework I might have built if I were smarter, more knowledgeable, and less lazy. Seaside isn’t the new kid on the web framework block. Avi Bryant and Julian Fitzell released Seaside in 2002, 2 years before DHH released Rails.

Here are a few Smalltalk resources for the intrepid adventurers among you.

Smalltalk.org
Squeak Smalltalk
Seaside Framework
WebVelocity with Seaside
Cincom VisualWorks Smalltalk
GNU Smalltalk
Croquet Consortium
OLPC Etoys

There are many free Smalltalk books available from Stephane Ducasse. Randal Schwarz, widely respected for his contributions to Perl (and the Schwarzian Transform) has become a persuasive Smalltalk advocate in his blog methodsandmessages.vox.com.

I believe that Smalltalk will gain increasing mind share in the coming years. Five years from now the general programming population will be thronging to Smalltalk courses in the local community college, as they are clamoring for PHP and Rails courses today.

2 comments