Class in Ruby, Simplified

Written by Supakorn Laohasongkram on August 14th, 2014

In our everday lifes, we almost unconciously define many things by class. For example, I am a class of Homo Sepian. (All I am saying is that I am a human! Am I?!) And within this class there are certain attributes and abilities which entails with being a Homo Sepia.

Similarly a Class in Ruby functions exactly the same way. Some of the pre-defined super classes in Ruby are Integer, String, Array, Hash and so on. And each of these classes have their own attributes and methods or functions.

You wouldn't ask a dog to speak because you know it can't. However, Homo Sepia has that function to speak; and in programming terms, human class has the method to speak. And you could very well "call" that method or its functions by telling someone to speak. This is much easier than telling a dog to speak right? Because if you do you are going to run into an argument error with the dog about how you are calling an undefined method. (This happens to me all the time, both when programming and when I am talking to my dogs. My dogs usually return me a default blank look and a wagging tail when I talk to them.)

Now, let's look some of the intricacies of how to use Class in Ruby and why. We will create a new class of alien!

Life Without Class...

								ET_Name = ET
								ET_From_Outer_Space? = true
								ET_Alien? = true

								Superman_Name = Superman
								Superman_From_Outer_Space? = true
								Superman_Alien? = true

								Yoda_Name = Yoda
								Yoda_From_Outer_Space? = true
								Yoda_Alien? = true							
							

As you can see, without class, if you want to define one alien. You always have to do it one by one. And that is not very efficient or very "DRY".

Life is Good with Class!

								class Alien #Defining Alien class 
									def initialize name
										@name = name #Store the instance variable of the Alien's name
									end

									def from_outer_space? # Method of Alien
										true
									end
								end

								ET = Alien.new 'ET'
								Superman = Alien.new 'Superman'
								Yoda = Alien.new 'Yoda'
							

Here, you can easily define ET, Superman, and Yoda have all the properties associated with being Alien by just setting each to the Alien class. So the simple point here is to demonstrate how class can be use to reproduce object which are similar to one another in a more effective manner.

Things one should know about class

Now that we learn a little bit about Class in Ruby. There are some important points which should be further mention.

  • method(s) which are defined within a class are called instance methods. By default, they can be used only for that particular class unless defined as 'global'

  • Instance variables also could be use only within the scope of the class which it is defined inside and no further. Instance variable could easily be written or identified by the "@" sign in front of the name of the variable like so: "@name = name".

  • Conclusion

    Class is a very important subject that one should get themselves comfortable with especially when using object-oriented programming like Ruby. For further information about Classes Ruby, you can go to the links on the Reference to your right! Keep calm and code on!


    © Copyright Supakorn Laohasongkram 2014