Ruby basics
Ruby, the object-oriented programming language used by the Rails framework, was created by Yukihiro "Matz" Matsumoto in 1995. Everything in Ruby is an object -- every variable and even such literals as "Hello World."
To begin coding a Ruby on Rails application, I first need RoR on my system. On Windows, Lenz recommends using Curt Hibbs' simple-to-run Instant Rails. Good idea. After a couple of minutes unzipping 18,000 or so files, Instant Rails quickly appears on my Windows laptop. Updating Rails is quick and painless with the command
gem install rails
--include-dependencies
Installing RoR on a Linux machine is a bit more manual. Without the complete Instant Rails package, you need to first install the Ruby programming language on your system, and then the Rails framework (which was written in Ruby by David Heinemeier Hansson as part of a project-management Web application called Basecamp). There are some links explaining how to install RoR on various Linux distros on the project wiki.
First impressions
I'm impressed by the elegance of some simple Ruby commands. Want to find the length of a string? Just apply the length
method to the string, with a period to separate the object from its method:
"Sharon Machlis".length
Type that into an interactive Ruby console and you get back 14
. (An interactive Ruby console will run commands as you type them in. Open a Ruby window and type the command irb
to get to interactive mode.)
Ruby commands don't require a semicolon at the end, which is appealing considering all the PHP and MySQL semicolons I've left off over the years. And if you use a method that doesn't need a value passed to it, you don't have to add empty parentheses like you do in a lot of other languages.
Ruby is more intuitive than other languages, developer Jay Powers at Vermonster LLC told me recently -- the code is easier to read and understand. He says it takes him less time to code major projects with Ruby on Rails than PHP or Perl because there are more methods built in.
That's the good news. The more daunting discovery is that it's initially more complex to set up classes than I'm used to in PHP.
When you define a class, you typically need some "instance methods" and some "instance variables" to store data. These variables always start with an @ symbol, as in @name
or @ssnum
.
Here's how Lenz does it for the class Car
(everything after each # mark is an explanation I've added).
class Car
# creates a new class named "Car"
@mileage = 0
# sets the initial mileage for each car to 0
def set_mileage(x)
# begins a method to alter the mileage
@mileage = x
# sets a new value for the mileage
end
# ends the method
def get_mileage
# begins "instance method" that lets us read the mileage data out of the car object
@mileage
# the method will return the value of this variable
end
# ends the method
end
# ends the class
@kitt = Car.new
# now we've got a car, the Car object named kitt
@kitt.set_mileage(5667)
# stores the mileage data of 5667 for kitt the car
@kitt.get_mileage
# reads the mileage information for kitt the car
At the outset, my old way of doing things, not using objects in PHP, seems a lot simpler:
$kitt_mileage = 5667;
# creates a PHP variable $kitt_mileage and sets it to 5667
echo $kitt_mileage;
# prints out the value of the variable $kitt_mileage
Hands on with Ruby on Rails
|
However, manipulating those variables down the road would end up being more complicated. My colleague and fellow programmer Joyce Carpenter assures me that the front-end investment in object structuring will pay dividends. I forge ahead.
Ruby has many typical features I expect in a programming language, such as arrays, associative arrays (often called hashes), strings, conditionals, loops and so on.
One point Lenz makes early: To loop through data in an array, Ruby programmers are more likely to use "blocks" than things like for/else/while/until loops. "However, they're also one of those things that take a while to 'click' for Ruby newcomers," he notes. He's right on that. I don't see how a construct like
[ kitt, herbie, batmobile, larry ].each do |car_name|
puts car_name.mileage #
end
is vastly superior to something like
$car_array = array("kitt", "herbie", "batmobile", "larry");
foreach ($car_array as $key = > $value){
echo "$value\n";
}
in PHP. I need to keep reading.