8 March 2015


Ruby Classes


In Ruby, the common cliche is, "everything is an object." And just as every living being in the Animal Kingdom belongs to a class, so does every object in Ruby.

Classes, or classifications, are used in Ruby to group objects that have similar traits and access to the same set of methods. Some predefined classes in Ruby, include: Fixnum, String, Array, and Hash. Each of these classes give rise to their own objects, and every object within a class has access to the same set of methods.

Where Ruby really gets interesting though, is the way it permits users to: create entirely new classes, define new methods within those classes, and create objects that can then use those methods. Ruby is an open book that has an existing framework that allows you to do pretty much anything you could ever want to do.

Creating A Class:

To create a class, start a new line of code with the "class" keyword followed by the name of your class. Make sure to begin your class name with a capital letter, as that's how classes are distinguished in Ruby. At creation, you can initialize a class so that anytime a new object is created within that class, the user must provide a particular piece of data that can then be referred to later. In this case, our new class is called "Rapper" and every time an object is created within the "Rapper" class a name must be provided for the new object.

class Rapper
  def initialize(name)
    @name = name
  end
end

As you may have noticed, at initialization we also took the "name" input and assigned it to a strange looking variable, called @name. This is an instance variable. You've probably already seen local variables, which can be defined within a method and set to a value. Local variables are great because they can be used later on within the same method; but that's it. If you want to use that same local variable in an entirely different method, you can't. That's because local variables are local to the method where they were created.

Instance variables are different. You define an instance variable within a class and can then use it anytime you want within that class. The key to distinguishing instance variables is that they all begin with an @ symbol.

Once we've created our class. We can define new instance methods that any object of the class will be able to use. Like instance variables, instance methods can be used at anytime within the same class. Here, we create an instance method called "hubris" that takes the instance variable @name and puts it in a string.

class Rapper
  def initialize(name)
    @name = name
  end

  def hubris
    p "#{@name} is the best! Way better than everyone else!"
  end
end

And finally, to tie everything together, we can create a new object (called "test") within our "Rapper" class and give it an input at creation. Then, we can call the "hubris" method on that object.


test = Rapper.new("Kanye West")
test.hubris


The output we get is unsurprising to say the least.


"Kanye West is the best! Way better than everyone else!"


And that's the basics of Ruby Classes!

- S.G.

Prev Next