1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Everything about Object Oriented Programming


Everything about Object Oriented Programming

Object Oriented programming is at the heart of Ruby. Ruby is, by definition, an object oriented programming languages. You won't be writing many programs without using classes, and even if you think you're not, even if you think you're just using Ruby as a "normal" programming languages, there are a hundred objects behind the scenes doing all of your work for you. The object oriented concept is the core of Ruby itself.

But what does this all mean? It's hard to sum up, you'll just have to keep reading. The following articles will get you started with object oriented programming in Ruby. They're ordered (roughly) for easiest to most difficult, and will give you a boost in your object oriented programming in Ruby. Even if you think you've got a handle on object oriented programming from Java or C++, keep reading, Ruby has a few different ideas when it comes to object oriented programming.

    • Introducing Classes does its best to introduce you to the concept of object oriented programming and to classes. While the subject can fill an entire book (and does, numerous in fact), the basics are summed up in a few paragraphs and you'll be off writing your own Ruby classes in no time.
      Methods are what classes, at their core, are all about. If you strip away all the idealism, and all the symbolism, and thinking in the problem domain, you just have a group of related methods that can all access the same instance variables. That's all classes really are. And here you'll learn about methods as they pertain to classes in Ruby.
      Using Attributes is a way to simplify your life. Tired of writing empty "setters" and "getters?" Attributes are one of the basic ways to interact with an object in Ruby. You have methods, which you can simply call like any other method, and you have attributes, which are a way to access instance variables. Since Ruby has no way to automatically access instance variables (such as public member variables in C++ or Java), you must use methods like attr_reader or attr_accessor to create the setters and getters for you, unless you like a lot of unnecessary typing.
      Object Specific Behavior is quite a common technique in Ruby. While most programming languages only let you change the behavior of a class by making a child class and changing the methods, Ruby not only allows you to add methods to existing classes, but to existing objects as well. This is kind of a hot topic, at least to object oriented purists who believe all objects of a certain type should behave the same, but that's idealism. There are a lot of idealistic things in Ruby, and there are some crazy off the wall stuff like this that can do a lot of good, or, as it were, be horribly misused. This is one of those features you have to learn how to use carefully, or you'll be pulling your hair out trying to debug your programs.
      Code Reuse: Inheritance is the bread and butter of object oriented programmer's toolboxes (who puts bread and butter in toolboxes?). All objects of a type act the same, and all object similar to that type act mostly the same. Inheritance is one of the base ways of reusing code and object behavior in Ruby (the other being composition and modules, see below).
      Modules: Containment and Composition will get you started using modules in Ruby. A module is something many programmers have a difficult time grasping at first. They're not classes, yet they have methods. They can be included in classes, or exist on their own. Most programming languages don't have anything like this at all. Simply put, a module is a named collection of methods. It's not quite a class, you can't instantiate it. It's not quite a namespace either, because it can be included in classes and become part of that class. It gets a bit confusing, because with modules and mixin modules, Ruby has a vastly different view of how object oriented programming should work than languages like C++ or Java.

    And there you have it. This should get you started with using classes in Ruby, at least. Don't get hung up on some of the details, like how when you call a method on an inherited class that Ruby knows to call the right method, or how mix-in modules work, all of that will be covered later. Right now, just focus on how to use these features.

    1. About.com
    2. Technology
    3. Ruby
    4. Beginning Ruby
    5. Object Oriented Programming
    6. Everything about Object Oriented Programming

    ©2014 About.com. All rights reserved.