Ruby

Ruby's Attribute Accessors

Nola Stowe wrote an article about attribute getters and setters recently, with comparsion between PHP and Ruby. Referring to Ruby's attr_reader and attr_writer, she said:

And, I like have getVAR setVAR. The ruby shortcuts don't allow that.

The Power of the Lambda

In one of the (non-Ruby) applications I maintain, there is a function that is responsible for handling unit conversions. It looks something like this:

double UnitConvert(double value, string from_unit, string to_unit)

So that I can do this:

double value = UnitConvert(5.0, "feet", "inches")

The underlying part of this code has to figure out exactly how to convert between the two units. In a nutshell, there's a big hash of known unit conversions that gets loaded when the program starts up, and it can interpolate, trace paths, and figure out how to fill in any gaps that may exist. In all actuality, it's a pretty smart piece of code.

Ruby 1.8.3

Ruby 1.8.3 has been released. The source is available here.

There have been some reports of it not playing well with established Rails installations, so be prepared to revert to 1.8.2 if necessary.

Why *you* need to learn Ruby on Rails

Editor's Note: For those of you who may have missed it, the Pragmatic Programmers have just launched a new series called "Pragmatic Fridays" and their first book was released last week. As an even more interesting note, the first one entitled "Rapid GUI Development with QtRuby" was written by our own Caleb Tennis.

In the movie "The Shawshank Redemption", after the character Brooks Hatlan got out of prison (after being cooped up for a very long time), he opines that: "...The world went and got itself in a big damn hurry". He wasn't able to cope in the outside world after living in prison for so long. He liked his old lifestyle better.

The same thing exists in software/tech world. In order to survive, you've got to stay afloat with knowing something about the latest and greatest happenings. No doubt you know that Microsoft is releasing a new version of Windows next year called "Vista". I'm sure you're aware of lots of the latest gadgets coming out of Steve Jobs' hands.

Well, if you're a web developer, no doubt you've heard of Ruby on Rails. But, more importantly, have you tried it?

The Saga Continues: Language tidbits in Ruby Vs. PHP

I wrote a simple math quiz game in Ruby, its one of the first things I wrote as a kid when I was learning GW-Basic (sigh, those were the days!). I found a few surprising differences in Ruby that I'm used to in PHP. Here are a few tidbits I discovered.

Increment Operators
You know these, its what you have in every for statement and quite often used in other looping structures. It PHP its used like $var++ and $var-- … which as we know, is to increase the value in the variable by one or subtract by one respectively. Well to my surprise (and failed Ruby code when I tried it) they aren't in Ruby. You have to use var += 1 and var -= 1 . weird huh? I wonder if this will change in future versions of Ruby. It's not THAT big of a deal, but I was surprised. I suppose it makes it easier later when you want to increase by 2 or 3. But, having a standard way to only increase by ONE is a good idea as a check against the programmer. What if they accidentally hit the 2 or 3 instead of a 1?

Agile Web Development with Rails

Many people (like me) want to start using Rails, but are a little intimidated by picking up a new language and framework. We don’t want to spend countless hours reading through online documentation and puzzling out how things work. We just want to get stuff done. Agile Web Development with Rails is written with this group in mind and it certainly delivers.

The book is targeted at people who are familiar with coding and object oriented design. While prior knowledge of Ruby is certainly helpful, the book works well for those who are new to the language as well as the framework of Rails (like me). For Ruby newcomers there is an appendix that covers some of the basics of the language. Let’s get on to the meat of the book.

Attribute Accessors - Ruby VS. PHP

Typically in objects you don't set/get the attributes directly, you use a method. This allows you to do type checking when setting the data or to format it a certain way when getting the data. In php I would use something like this:

    function setAttack( $atk ) { $this->attack = $atk; }
    function getAttack() { return $this->attack; }

And used like this:

$testcard->setAttack(4);
echo $testcard->getAttack();

Ruby Editor/IDE Comparison

Last week I posted about my preference for IDEs. Well my adventures in Ruby continue and with them, my search for the right development environment. What's the best environment to get Ruby on Rails development done? Well, David Heinemeier Hansson (the architect of Rails) has sounded off on his preference for TextMate, but what about those of us not working on OS X? I took a little bit of time and played with jEdit, Eclipse, UltraEdit, gVim, ArachnoRuby, and SciTE. Read on for my initial thoughts on each.

Extending Objects - Ruby VS. PHP

Extending a base class to a more specific class is a good practice and is called "inheritance" Here's the process I go through to decide if I should make a base class and extend or not.
I make a list of the objects I will need for a game, for the VS game we have:

  • Character cards
  • Plot twist cards
  • Location cards
  • Equipment cards
  • Player
  • Board

Those items that have "card" that gives me a clue that maybe they are related and perhaps have some common attributes. Then I make a list of the attribute of each card. I'll just show two of the card types here:

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.