Strings are a Domain-Specific Language

Question: Isn't a domain-specific language just the same thing as a library? (Source: Pretty much everyone the first time they hear of DSLs.)

Answer: No, a DSL is much more than a library, and I have an example that won't make you say, "Well, sure, if you're doing something that esoteric..."

Reason to like PHP?


print { open my $out, ">", "pidfile"; $out or die } $$, "\n";

Ruby;'pidfile', 'w+'){ |fh| fh.puts }

I could have used ‘$$’ instead of, but I prefer readability


file_put_contents( 'pidfile', getmypid() );

Yeah I know, I know but anyway…

This article provided by

Ruby on Rails makes nice with PearDB

Another exciting day in the life of a PHP -- > ROR programmer..

Last week I wrote about how Ruby could access a PHP session and how php site structure could fit inside a Rails structure. This week, I share my findings on how to get ActiveRecord to work with our existing PearDB sequence tables. These are my random, scattered meanderings on a solution..

If you haven't used Pear's DB class before, it creates a table with the [tablename]_seq for each table and stores a single row with a single column "id" with the value of the last ID used. Example of use. This was designed so that your code could easily port to other databases that didn’t have autoincrement like MySQL. Whether this is a good practice is not up for debate, I have what I have :)

Ruby on Rails makes nice with PHP

Not all projects can jump ship from a home grown PHP site to Rails, especially projects where there are exactly 1.3 programmers for the project. I was surprised there wasn't much information on integrating php and rails site (spare me the flame wars). All new development will be done in Rails and old PHP will be written in Rails as time permits. I was exploring some of the ways that this could be possible and what possible problems might arise.

I will need to read certain sessions variables from PHP into Rails, I will not need to write to the session. I found a ruby script PHP Serialize which takes a php serialized string and converts it to a Ruby datatype. If your sessions are stored as files, they are in /tmp/sess_[session id] where session id stored in $PHPSESSID they contain a special serialized string. Ruby can get the session id from the cookie and read the file, and PHP Serialize will convert it into an object. If your sessions are stored in a database, then of course you'll be able to create a Rails model to access it. If you need to write to a php session, then I think it would be better this way. I didn't try using sessions stored in a database, because I use files. I’m not sure how you would do it stored in memory. In any case, reading from the session file I believe is a temporary hack at best.

Learning Design Patterns – Iterator Pattern

This week I'll be stepping through the Iterator Pattern in my series on design patterns plucked from this book and from examples on the Internet.

What is it?
The Iterator Pattern encapsulates the functionality for stepping through a collection of items. The iterator object implements the stepping; the aggregate itself does not step through its own collection.

The pattern generally has a few of methods like first(), hasNext(), Next(), isDone() in some combination. In some programming languages it is possible to step through an iterator using a For Each item in Aggregate....<code>....Next type of construct.

Where is it used?
Anywhere there is a group of items. An array list is a common example; an iterator would step through the array items until the end of the collection was reached.

PHP can be cool like Ruby

All the cool kids these days are using Ruby...however I am stuck in a PHP world at the moment. I've been doing Ruby on some side projects and its got me thinking in a ruby mindset...and I wonder.. can PHP do that? And I've found a few things that might make a PHP geek's life a bit easier or at least PHP a little more tolerable.

Yes yes, the good old for loop.

for ($i = 1, $i <= 10; $i++) {
    print "Processing $<br>";

The simple act of doing something 10 times. Ugly huh? He's one way to do it in Ruby..

A look at Rails' Code Generation and Java

Bill Venners of Artima recently posted a look at how Rails' code generation compares with Java, and talks about Bruce Tate's recent DeveloperWorks article on the comparisons between Hibernate and ActiveRecord.

Learning Design Patterns – Template Method Pattern

This week I'll be examining the Template Method Pattern in my series on design patterns plucked from this book and from examples on the Internet.

What is it?
The Template Method Pattern lays out the steps in a process (algorithm) and allows subclasses to handle one or more of the steps internally. The pattern has a method that just contains a list of method calls to complete the process (create something, package it, sort it, deliver) that can not be overriden or augmented. Some of the process methods may be handled by the pattern (create something and deliver), while it makes the subclasses handle the others (sort it, package it).

Learning Design Patterns – Adapter Pattern

This week I'll be switching over to the Adapter Pattern in my series on design patterns plucked from this book and from examples on the Internet.

What is it?
I remember when I was about 6 I was helping Dad replace a section of board-fence with chain-link fencing (by helping, I mean I was standing there picking my nose and asking him inane questions). He had taken the old fence out, which left three round holes in the ground where the fence posts were. Maybe to quiet me, or maybe just because he enjoys being ornery, he asked if I could explain how those square fence posts had fit into the round holes we were staring at. That question stumped me for years (I blame that on all the lead buckshot I ingested from the pheasants Dad hunted), but now I see that the Adapter Pattern is the answer (or not, but it makes a good story, right!?)

Learning Design Patterns – Command Pattern

This week I'll be taking charge of the Command Pattern in my series on design patterns plucked from this book and from examples on the Internet.

What is it?

A simple analogy for the Command Pattern is a remote control. When you are clicking through your 637 channels looking for something in English that isn't tying to sell you something or change your political or religious point of view, you're giving commands to your remote, which then tells the system to increment the channel by one.

And when you use a universal remote and set your TV to channel 3, your VCR to channel F1, your sound system to Video, and then turn on your DVD player to watch a movie you rented from Blockbuster because nothing is on TV; your remote is issuing commands like "On" to each device, and the device is responsible for taking care of the details of turning itself on. If your remote was hardwired to turn on each component, you would have to open it up and add more code each time you switched out one TV for another – that's tightly coupled!

The Command Pattern can include more than just the ability to encapsulate requests; it can also queue up commands and log them, and support an "Undo" operation. By extending the pattern slightly, you can also create the ability to run a macro command that executes several commands at once – imagine a universal remote that has an "all-on" button that turns all of your devices on at once.