Ruby

Data Access Objects - Rails Style

Most other DAOs you have to create some configuration file, whether xml or ini files or created manually. Example, to setup Pear's DB_DataObject, you must create an ini file and run a script "createTables.php" each time your database changes. Not so with RoR! its automagically created and updated for you. It does ALL the basic CRUD for you. Just have to specify the relationships (if any).

Say you have tables:

CREATE TABLE people (
id int(11) NOT NULL auto_increment,
name varchar(255),
PRIMARY KEY (id)
);


CREATE TABLE companies (
id int(11) NOT NULL auto_increment,
name varchar(255),
PRIMARY KEY (id)
);

Naming Convention
By the way, RoR has a naming convention for tables. You name the table the plural version of the word. The primary key has to be ID in each table, foreign keys are _id. EGADS! It almost looks like English, huh? An average half-way intelligent person could even read it and have a clue of what’s going on. Even pointy haired bosses. Though, in traditional fashion, some people have to complain about having to name their tables a certain way. (violin music playing) There is a way to turn off "plural/singular" in the settings. So if you just have to be difficult, here ya go.

A guide for changing programming languages

Many projects will encounter a point of frustration with some aspect of Information Technology architecture and developers may clamor for a new technical solution. Changing programming languages may involve a significant rewrite of the software, which is a bad idea most times. However, if you are faced with a compelling technology how should a Project Manager approach the transition? Two personal examples may shed some light on the decision whether or not to change programming languages.

Ruby for the PHP Programmer ...

This will be a series of my findings in the syntax and language of Ruby from a PHP perspective.

Displaying and formatting output is one of the first things you need to know when learning a new language.

PHP:

echo Look it up at PHP.net
echo value [,values];
This is not a function, so ( ) is not needed. It is a language construct. Hmm, something I just learned. it will concat the variable for you and display. This might come in handy, sometimes is a pain to always do print $var1 . " " . $var2; Sometimes those periods are easy to forget!

Example:

$first = "Nola";
$last = "Stowe";
echo $first, ' ', $last, "<br>";

print Look it up at PHP.net
print value;
Also not a function but a language construct, so () not needed here as well.

Example:

$first = "Nola";
$last = "Stowe";
print $first." ". $last."<br>";
// or
print "$first $last <br>"";

results in:
Nola Stowe

For more on the differences of print and echo, read: http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40

In most cases, I usually use print.

Also availble in php, but I never rarely use:
printf (string format, [, mixed args]); uses a formatting string
sprintf (formats as printf, but reutrns a string rather than printing)
vsprintf (like sprintf, but accepts an array of values instead of a variable number)
vprintf (like printf, but accepts an array of values instead of a variable number)

Ruby:
(which BTW, does not require ; at end of line, local vars don't have $ those are reserved for globals)


first = "Nola"
last = "Stowe"


print first, ' ', last, "<br>"

prints contents of variable test followed by test2

results in:
Nola Stowe

However, it appears some special variables (lookie! a $!!! that means they are global)


$, = " " #displayed between variables in print
$\ = "<br>" #displayed at end of the line

Will set some defaults for the print statement, so once those are set, all you need to do is:


print first,last

To get the same result. Cool, huh?

Also in Ruby I found puts, and putc. I need to do some more investigation on those. Perhaps a reader can shed some light?

So you can see, PHP's echo is most like Ruby's print. That should help us die hard php programmers who normally use print exclusively.
Also in Ruby, which seem to work like the php versions:
printf
sprintf