Ruby Classes: How to be Classy like Tony Stark

06.17.15

What is a ruby class? No, it's not a course about a precious stone at your local community college. In Ruby, a class is a useful way to organize multiple methods and objects. Ruby classes is a broad subject, with many features like class variables, class methods, instance variables, etc. In this post, I will focus on the basics of class creation, how to make an instance of that class, and how to make instance methods. I will also introduce the concept of class inheritance and how to use it.

Using classes, you are able to easily spawn multiple objects that share properties. A new object created through a class is called an instance of that class. As usual, I will make things easier via examples. lets say we want to make a new class called Ironman (by convention, class names are Captialized), we would type:

    class Ironman
        def initialize(name)
            puts "J.A.R.V.I.S Says: Hello #{name}"
        end
    end
    

And we can create an instance of the Ironman class like this:

tony_stark = Ironman.new("Tony Stark")

When we created a new instance of the Ironman class by calling Ironman.new, we made a new Ironman object called tony_stark, and we see J.A.R.V.I.S greeting him. But so far tony_stark doesn't do anything, so let's add some methods to the Ironman class:

    class Ironman
        def initialize(name)
            puts "J.A.R.V.I.S Says: Hello #{name}"
        end

        def repulsor_beam
            puts "Pew Pew PEWWW"
        end

        def insult
            puts "Your are a ginormous !!@$@!$!@#"
        end
    end
    

now when we call tony_stark.repulsor_beam, the console will print "Pew Pew PEWWW", and when we call tony_stark.insult, well...you get the gist. The methods repulsor_beam and insult are instance methods, and any instance of Ironman will know how to use them. If I make another Ironman object called jerry_chai, then jerry_chai will be able to shoot repulsor beams and call you names.

But alas, there can't be more than one Ironman, and poor Tony is lonely, so lets make a few more avengers for him to play with.

    class Thor
        def initialize
            puts "Fear not Midgard, the mighty Thor is here!"
        end

        def hammer_toss
            puts "KLUNK!"
        end

        def lightning
            puts "KA-BOOOOOM!!!"
        end
    end

    class Hulk
        def initialize
            puts "I'm getting angry. You wouldn't like me when I'm angry."
        end

        def smash
            puts "HULK SMMAAASSSHHHH!!"
        end
    end
    

There, now we just need to thaw Captain America from that block of ice and we can form the Avengers. But wait, what if we want to have a method that all the Avengers know how to use? We can add it to each of the three classes we made, but that's a lot of copying and pasting. This is where inheritance comes in handy. But first, lets make the Avengers class:

    class Avengers
        def initialize
        end

        def assemble
            puts "Avengers! Assemble!"
        end
    end
    

Now we can have the three Avengers inherit from the Avengers class:

    Ironman < Avengers
    Thor < Avengers
    Hulk < Avengers
    

And now all three classes will know how to use the assemble method. I mean, the Hulk probably wouldn't use it much, but it's there if he wants it. In this context, Avengers is a superclass of Hulk, and Hulk is a subclass of Avengers. Ruby differs from some other object oriented languages in that it does not allow one class to inherit from multiple classes. So if Thor wants to be a member of the Avengers class, he can't also be a member of the Asgardian class. Sorry Thor, sometimes you just gotta choose.

Classes and inheritance are useful tools that give structure to your code. But be sure to think ahead when you are organizing your classes. Badly organized classes make it harder to read your code, which makes debugging and updating your program an utter nightmare. Kind of like how the Avengers is a nightmare for their enemeies.