Ruby Self-Tutorial, Day 1 of 21: Getting Started With Ruby

Saturday, March 26, 2005

(note: this is part of a 3 week series)

For Those About To Rock

Here we go - I've been itching to get this project started for weeks and the big day is finally here. I completely blanked on it being Easter Sunday though, so if y'all can put that aside and get started with me anyway, I'll try to forget that the U.S. lost to Mexico in a 2-1 nailbiter this morning. Man, it was a World Cup qualifying match too, we really needed that. Although they came on strong in the second half, they just looked plain sloppy in the first. Being Turkish-American, at least I can take solace in Turkey winning against Albania.

Day 1 - Getting Started With Ruby

The good news is that today is a relatively light day, as to be expected from an introductory chapter. From the book:

Today we'll talk some in general terms about what the Ruby language is, find out where to get it if it's not already installed, and take our first simple programming steps.

I can help out with getting Ruby installed for those of you without it - which means mostly Windows users since OS X kids already have it - by pointing you to Curt Hibbs' tutorial on Ruby On Rails. You don't have to go through the whole thing if you don't have the time (it really does give you a neat little taste of the power of Rails) and can just jump down to the section on "Installing the Software". That's how I got Ruby, Ruby On Rails and MySQL installed on my Win box and it was painless. Believe me, I'm no UNIX software-installing expert and it went fine.

Results

Today starts off with a bit of an intro to the characteristics of Ruby, as opposed to the more historical descriptions in the previous Introduction chapter. Since anyone actually doing the course is obviously someone that knows enough about Ruby to be interested in learning it, there's no need to preach to the choir too much - we're already sold on it, thanks. However, there's some points that stood out worth commenting on.

First, the assertion that Ruby is "Small And Intuitive". The word "intuitive" is a bit loaded - different things are intuitive to different people - but the concept of a "small" language is interesting. The definition offered is that Ruby is small because it doesn't require a lot of memorization, which implies that you can learn just a few things and go all crazy combinatorial with them. Now that intrigues me a lot and I'm totally sold on the idea of simple yet powerful, the philosophy of using simple things that combine to produce complex results. It's very Stephen Wolfram (A New Kind Of Science) and Steven Berliner Johnson (Emergence), like simple recursive mathematical formulas that produce beautiful fractal patterns or many tiny stupid ants that build smart colonies of incredible sophistication, complete with cemeteries, landfills, gardens, livestock farms and heating systems.

This section also introduces the concept of POLS - the principle of least surprise, asserting that Ruby avoids the frustration of those with "Do What I Mean!" stickers plastered on their monitors.

Ruby is an object-oriented programming (OOP) language. I won't open the can of worms of whether that is clearly a good thing or not, I'll just say that I'm a fan and observe that the OOP concept is familiar to Flash developers, since our code can actually target specific objects on the screen and not just the abstract concept of an object. I've found that really helps in going from procedural languages to wrapping your noggin around the OOP workflow. Manipulating the DOM with Javascript is similar, since you're targeting a specific piece of markup in a web page. According to the authors, Ruby's "internal logic" (whatever that means) implies that despite similarities to Perl, Ruby is more like a pure OOP language called SmallTalk. Considering the praise heaped on languages like SmallTalk and Lisp by the likes of uberprogrammers Dave Thomas (Programming Ruby) and Paul Graham (Hackers And Painters), this comparison made me pay attention to the rumblings I've been hearing about Ruby being immensely learnable yet powerful. Maybe it's not hype after all, eh?

Writing the first script was interesting! At the end of each line, I had to slap away my right hand from the keyboard with the left: I was trained like a monkey to put a semicolon and it was a difficult urge to resist. Resist it I did, though, and then smile to myself after realizing the keystroke was superfluous. I had a brief hallucinatory flashback of all the stupid semicolons I'd typed before, a line of pixels stretching out to a distant horizon behind me, my right index finger tapping out a constant stream of the buggers. All the wasted seconds and energy I'd spent, imagine what that adds up to!

Playing with the dot notation of accessing methods (string_variable.length, string_variable.reverse) was a welcome familiarity from Actionscript and Javascript.

The string reversal reminded me of a bit of trivia: my full name backwards is Tuba Repla. What's yours?

I much prefer the #{variable} way of introducing variables into a string rather than the ", variable, " equivalent. For example, this:

print "Backwards, it reads #{user_input.reverse}.\n"

Reads better to me than this:

print "Backwards, it reads ", user_input.reverse, ".\n"

I never really liked breaking up the double quote marks, it just felt wrong and made the damn things to hard to read. The first line is how I see the problem in my head - the sentence isn't broken up into strings and variables. Ah well, inherent readability amongst languages is an endless debate, no need to trigger it here, put your guns away.

The Using Ruby Interactively section was very cool. It was like having a little conversation with my iBook. No saving a text file, flipping over to the terminal and then running the damn thing - you just enter it right in and watch it run runnity run. The book says this is a great technique for testing out sections of code. For me, it was also a nostalgic reminder of how I got hooked on programming in the first place, way back when I was a little kid typing BASIC commands into a Texas Instruments console hooked up to the TV. Write a few commands and bam, watch it print stuff to the screen. There's always been an intermediary step - I haven't done it directly in the command line in years! I feel the same sense of play as I did back then too! Okay, I'm officially gushing too much.

And thanks to that exercise, I now know that I'm running Ruby version 1.6.8 on the little guy. I have no idea how current that is and don't even come close to caring; I can upgrade it later on if need be.

IRB was rad, it's like the "use tracer bullets" methodology of the Pragmatic Programmer book. Tracer comments are something every Actionscripter is familiar with - it's a life saver when isolating the effects of a section of code. IRB is like that, but throughout the program, on every line of code. My program's riddled with bullets!

The link to the eval.rb script is broken - download it here instead: http://www.cs.iastate.edu/~slagell/ruby/eval.rb

Here's the related and useful explanation of what the hell eval.rb is and how to use it. It's just easier to see what it does (color coding the output in your terminal window) and takes but a second to try out.

Homework

You fire up irb and enter a bunch of lines into the command line, while observing the resultant output, then there's 9 simple questions afterwards. I got 6 right, 1 wrong, 2 blank-cause-I-don't-know.

I got number 4 wrong - I mistakenly remembered irb as saying that the string "99" was less than "100" and rationalized it as being because "99" had less characters than "100". The correct answer is that "99" is greater than "100", since strings are compared alphabetically and 9 is greater than 1 in that sense, just as "Wilma" is greater than "Fred".

I blanked on number 6 - I wasn't sure if "hubba " * 2 would come out as 12 (a 6 character string times 2) or "hubba hubba " and the answer was the later, "hubba hubba ". Multiplying a string replicates and concatenates it.

I also didn't know number 7, which says that <=> gives -1, 0 or 1 as values, rather than true or false. To be explained in later chapters.

More Useless Personal Observations

Something I noticed while doing the homework: I love that you put a question mark at the end of a command that's asking something. It feels so human - sometimes we make statements while talking, asking questions at other times, it goes back and forth. That's called a conversation.

After posting this morning's chapter summary, I drove down from my girlfriend's folks place in Sylmar (north Los Angeles) and didn't settle in to read the chapter until 9pm. I'm happy to report that doing the reading, completing the exercises and writing it up took no more than an hour and half, so the workload definitely looks manageable so far. If any day was going to test whether real life was going to derail the course, today was a good candidate: wake up with a twisted neck on a couch made for midgets to a full and bustling house on Easter Sunday, watch a World Cup qualifier, catch up with some client work and drive for several hours.

Granted, we're just dipping our toes in easy material so far, let's see how challenging later chapters get, but I still can't resist: w00t!

shoutouts: Garrick's Day 1

elsewhere on the web: , , ,

FWIW, Ruby 1.8.2 is current. There are a few significant differences (see http://whytheluckystiff.net/articles/rubyOneEightOh.html) but I think Sam's Teach Yourself Ruby is written against 1.6.
Cool, thanks Dave. Then it should be fine to use throughout the book. Maybe by the end of it I'll be able to understand what those 1.8.2 updates are and why they matter :)

Post a Comment

-->