Objects - Ruby vs. PHP

I was teaching my husband some programming in PHP (meanwhile, I’m learning Ruby) and wanted to find something he was interested in to use for examples. He is an avid player of the game VS; and I have resigned to a "if you can't beat 'em, join 'em" mentality and started to play too (Geez, I was tired of being ignored!). It’s a pretty complex game, but kind of fun once you learn all the rules.

I found it to be a pretty good problem area for teaching. There are plenty of if/else statements to learn about conditional statements, operators and entities that can serve as objects and interaction between card, player, opponent etc.

I’ll keep it simple, there are few more attributes I could put in this class, but this suffices for illustration of Ruby and PHP.

A basic card has a name, cost, text and id string.

Here’s the code for PHP:

class Card {
var $cardID;
var $name;
var $text;
var $cost;

function Card($cid = "", $name = "", $text = "", $cost = 0) {
$this->cardID = $cid;
$this->name = $name;
$this->text = $text;
$this->cost = $cost;

function dump()
print "ID: {$this->cardID} Name: {$this->name} Cost: {$this->cost}Text: {$this->text}";
} // end of class

Note that the constructor is the same name as the class and initializes the member variables. Member variables are accessed with the -> operator.

Notice in the dump method, I used { } (referred to as up moustache and down moustache by MrNick) to "interpolate" the value of the variable in the string. Otherwise you'd be printing the name of the variable, rather than its contents.

To instantiate the PHP class

// set variables, in future retrieve values from database
$cardID = "DOR-021";
$name = "Tim Drake <> Robin";
$text = "Activate -> Target Gotham Knights attacker gets +X attack this attack, where X is the cost of that attacker.";
$cost = 2;

$testcard = new Card($cardID, $name, $text, $cost);

Usually when I write a class, I have a method name "dump" or "debug" that will allow me to dump out what values are set in the object. I can test my class early on in the process and later when I have more complexity. For now, I can confirm the constructor is working properly and I have all my variables assigned correctly.

With Ruby, the same concept (php is spitting out HTML output in my browser, with ruby I'm just running this ruby script on the command line for now)

class Card
def initialize(id, name, cost, text)
@id = id
@name = name
@cost = cost
@text = text

def dump()
puts "ID: #@id Name: #@name Cost: #@cost \nText: #@text"

And to instantiate the Ruby class

id = "DOR-021"
name = "Tim Drake <> Robin"
text = " Activate -> Target Gotham Knights attacker gets +X attack this attack, where X is the cost of that attacker."
cost = 2

testcard = Card.new(id, name, cost, text)

Interpolation in Ruby is just simply the # sign before the variable.

Note the constructor is "initialize" and the class is created as if it had a method of "new" somewhat unusual amongst OOP languages that I know, but hey that’s alright.
I could have used the to_s method to "dump" the variables, or I could have overridden the to_s method with my own display method. Normally I would have done this, since it is convention that a "to string" method will display the object in string format, but for the sake of comparison to my php class, I made a new method dump.

(sorry my code indents aren't showing up, I indent.. really I do!)

Next blog entry, extending objects in php and ruby.

Interesting post since I

Interesting post since I don't know PHP and am just dipping my toes into the Ruby swimming pool. Along those lines, I have a ruby newb question. What are you using to do your dev work with ruby? Have you had to debug any ruby code yet? As I'm starting out I'm trying various tools for developing with ruby and I haven't yet found the perfect fit.

Tell me what sort of editor

Tell me what sort of editor features you are looking for, if see anything then I'll keep ya posted!

Here's an option: http://www.jedit.org/ruby/ ... Sitepoint has an article on using JEdit for php that was written by a gentleman from the Chicago PHP Group!

Edited: Didn't have any luck with JEdit... said the ErrorList plugin need to be updated, so I downloaded the latest zip of that file and the archive was corrupt. Bah. :/

I also had problems with

I also had problems with jEdit. Couldn't get the thing to work and gave up after only a little bit of tinkering since I wasn't too impressed with the UI. I've always hated the java non-native UIs. I'll probably spend a little more time with it next week. For the moment I'm testing out ArachnoRuby which is actually working better than any of the other options I've seen.

It works!

Thanks, I installed the Error List, Project Manager and LookAndFeel from the plug in manager. At least I don't have any more errors. I'll experiment some more with it. Thanks for the tip Scott! Hey Paul, lets give it another shot.

Sounds Good

I'll be jumping into it tonight or tomorrow. I'll let you know how it goes.

I've got it all set up and

I've got it all set up and things are looking pretty sweet. I can't get the LookAndFeel thing to work right. Just spent about an hour looking online and I see that other people have the same problem but I haven't been able to resolve it yet. At any rate, this looks pretty good and I'll give it a spin for the next week or so.