Home > Articles > Programming > Ruby

The Meta-Guide To Learning Ruby

  • Print
  • + Share This
Really mastering a new programming language, certainly one as flexible and fluid as Ruby, is not easy. Russ Olsen offers some tips to ease the pain and speed yourself down the road to Ruby mastery.
Like this article? We recommend

Like this article? We recommend

I've been teaching Ruby off and on for a number of years. Actually that's not really true. It would be more accurate to say that I've coached people into teaching themselves Ruby. I don't think that anyone has ever really been taught a programming language: Learning a new programming language is one of those things that you need to do for yourself. Sure, you might have a cheerleader like me in the room while it is happening, someone who can help out a bit by saying "Look at this" or "Try that," but in the end you need to do the looking and you need to do the trying.

The key word here is trying. Really mastering a new programming language, certainly one as flexible and fluid as Ruby, is not easy. Ironically, it's the people who have really mastered some other language that seem to have the most trouble. It's a long fall from knowing exactly what you are doing, from building multi-threaded servers to stumbling on HelloWorld. Still there are things that you can do to ease the pain and speed yourself down the road to Ruby mastery.

Start by Focusing on the Familiar

The first thing is to build on what you already know. Fortunately, this is easy when you are learning Ruby. For example, I usually start teaching Ruby by showing some code and then asking a few questions. Here's the code:

class Employee
  attr_accessor :first_name, :last_name
  attr_reader :active
  def initialize( first_name, last_name )
    @first_name = first_name
    @last_name = last_name
  def hire
    @active = true
  def fire
    @active = false

The questions are simple: What is that? There's usually some mumbling at first but it quickly resolves itself into a very clear answer: that's a class definition. How many methods? This time the answer is quicker: three. Which one do you think is the constructor? No hesitation now: The initialize method. And off we go.

I can get away with this because on one level Ruby is a very mundane object-oriented language. Anyone used to the ideas of OO programming will have no trouble deciphering my little example or any other simple Ruby class. The mundaneness of Ruby runs pretty deep: Ruby classes have instances, and an instance has exactly one class – no multiple inheritance allowed. When it comes to places to put data, Ruby has the familiar menagerie: there are local variables, method arguments, instance variables, and class variables. You call methods with the familiar object.method syntax. The best way to get started with Ruby is to focus on this familiar common ground. Look for the ways in which Ruby is exactly what you would expect and grind those bits into your psyche. There is no quicker way to learn something than to already know it in the first place.

Now Ruby is not exactly the same as your previous language. There are endless Ruby-esque flourishes on all of the familiar ideas: Ruby's single inheritance model allows for module inclusion, which is sort of like multiple inheritance. Ruby gives you a couple of different ways to hang data on a class. Ruby method calls don't generally require parentheses, so that along with the familiar puts( 'hello world' ) you can also say puts 'hello world'. The trick is to avoid getting stuck on the differences and focus on the things that are the same, at least in the beginning. You can build up a fair size kernel of Ruby skills by simply saying, “Oh yes, that's a class, I know what a class does.” Wait until you have built up a base of competency before you branch out into the strange stuff.

Don't Sweat the Small Stuff

And strange stuff you will find in profusion in Ruby. Some of the things that new Rubyists find strange about the language are deep and important; other things are shallow and trivial. We'll talk about the important stuff in a bit, but for the moment let's spend some time on the strange but trivial. Take the way that Ruby programmers indent their code: In Ruby, the rule is that you use two spaces for each level of indentation. It's always two spaces per level, never four, never eight or any other number. And it's always spaces, not tabs. This bugs a lot of new Ruby programmers. Programmers coming to Ruby from Java also tend to get irritated by the Ruby convention of using snake_case instead of camelCase names for variable names. And a surprising number of people are put off by the fact that the middle keyword in a multi-branch if statement is elsif, without the 'e'.

I can sympathize with all of this angst – in my early days I felt the same way. My suggestion for dealing with these little irritations is simple: Let yourself vent for a few minutes:

    Spaces! Really?

    But camelCase is so much better looking!

    Are they charging an extra $10 per vowel these days?

Let yourself go like this for a while and then move on. When you are learning a new programming language it is the trivia that tends to hit you in the face, but in the end it is just trivia. You weren't born with a tab key at the end of your finger. You learned to love camel case. No one has ever been confused about exactly what elsif means. Every language presents these kind of niggling irritations to newcomers, and the wise newcomer copes by simply working through the pain. You aren't learning Ruby because you love two space indentation or variable names with underscores, so don't reject it because of them. By the time you really understand the language you will either love it or you will hate it, but chances are that the spelling of elsif will not play into your decision.

Do Sweat the Big Stuff

Okay, so you've learned the parts of Ruby that seem familiar and you've managed to keep from putting your foot through the LCD over that missing 'e'. Now what? Now is the time to dig into the weird and important bits of Ruby.

Despite appearances, Ruby is not a mundane programming language. Ruby allows you to create instance variables, methods and classes on the fly: You can literally reprogram your application as you go. Ruby code can respond to calls to methods that have never been – and will never be – written. On top of this, every Ruby object has its own private class that lets it do things that no other object can do. As I mentioned, Ruby's modules give you a close approximation of multiple inheritance. Module inclusion also lets you rewrite the inheritance tree of any object, as your program is running. Oh, and Ruby is also a dynamically typed programming language, which means that it's the objects themselves and not the variables that refer to them that know about type.

All of this is why you don't want to waste your time on the syntactical trivia. The semantic power of Ruby is where the real action is and where you want to spend your energy. Try to keep an open mind as you work through all of these odd Ruby ideas. Remember, a very large number of programmers think that Ruby is a wonderful, elegant, fun-to-use tool. Many of those programmers – including me – came to Ruby after years of experience with more traditional programming languages. It is certainly possible that in the end you may decide that we are all mistaken, that we are all wrong. But we are not all nuts. The decisions that are built into Ruby go to the heart of how a programming language should work and what it should be. If you are going to take the time to learn Ruby, suspend your disbelief long enough to see it through the eyes of those who really like it. Then decide what you think.

There Is No King's Road to Ruby

As I said, the only person who is going to teach you Ruby is yourself. Sure, take a class (I know a guy...), read some books (I have a couple of suggestions...), and Google your way around the Internet. But do remember that mastering a new language means writing code, lots of code. That's why they call them programming languages. As you write each new program, build on the familiar aspects of Ruby and try to ignore the irritating parts. Most of all, challenge yourself to see what it is about this funny little language that makes so many of us so passionate about it.

Russ Olsen is the author of the upcoming book Eloquent Ruby, which, among other things, explains the solid engineering reasons behind Ruby's two space indentation rule, why dynamic typing is your best friend and how you might use the other crazy-sounding Ruby features. Russ's first book is the highly regarded Design Patterns in Ruby. Russ, who is now celebrating his second decade of Ruby programming, lives outside of Washington DC and at russolsen.com.

  • + Share This
  • 🔖 Save To Your Account