Last modified on 7 February 2012, at 02:05

Ruby Programming/Classes and objects

← Writing methods | Exceptions →


Ruby ClassesEdit

As stated before, everything in Ruby is an object. Every object has a class. To find the class of an object, simply call that object's class method. For example, try this:

    puts "This is a string".class
    puts 9.class
    puts ["this","is","an","array"].class
    puts {:this => "is", :a => "hash"}.class
    puts :symbol.class

Anyhow, you should already know this. What you don't know however, is how to make your own classes and extend Ruby's classes.

Creating Instances of a ClassEdit

An instance of a class is an object that has that class. For example, "chocolate" is an instance of the String class. You already know that you can create strings, arrays, hashes, numbers, and other built-in types by simply using quotes, brackets, curly braces, etc., but you can also create them via the new method. For example, my_string = "" is the same as my_string = String.new. Every class has a new method: arrays, hashes, integers, whatever. When you create your own classes, you'll use the new method to create instances.

Creating ClassesEdit

Classes represent a type of an object, such as a book, a whale, a grape, or chocolate. Everybody likes chocolate, so let's make a chocolate class:

    class Chocolate
      def eat
        puts "That tasted great!"
      end
    end

Let's take a look at this. Classes are created via the class keyword. After that comes the name of the class. All class names must start with a Capital Letter. By convention, we use CamelCase for class name. So we would create classes like PieceOfChocolate, but not like Piece_of_Chocolate.

The next section defines a class method. A class method is a method that is defined for a particular class. For example, the String class has the length method:

    # outputs "5"
    puts "hello".length

To call the eat method of an instance of the Chocolate class, we would use this code:

    my_chocolate = Chocolate.new
    my_chocolate.eat # outputs "That tasted great!"

You can also call a method by using send

    "hello".send(:length) # outputs "5"
    my_chocolate.send(:eat) # outputs "That tasted great!"

However, using send is rare unless you need to create a dynamic behavior, as we do not need to specify the name of the method as a literal - it can be a variable.

SelfEdit

Inside a method of a class, the pseudo-variable self (a pseudo-variable is one that cannot be changed) refers to the current instance. For example:

    class Integer
      def more
        return self + 1
      end
    end
    3.more # -> 4
    7.more # -> 8

Class MethodsEdit

You can also create methods that are called on a class rather than an instance. For example:

    class Strawberry
      def Strawberry.color
        return "red"
      end
 
      def self.size
        return "kinda small"
      end
 
      class << self
        def shape
          return "strawberry-ish"
        end
      end
    end
    Strawberry.color # -> "red"
    Strawberry.size  # -> "kinda small"
    Strawberry.shape # -> "strawberry-ish"

Note the three different constructions: ClassName.method_name and self.method_name are essentially the same - outside of a method definition in a class block, self refers to the class itself. The latter is preferred, as it makes changing the name of the class much easier. The last construction, class << self, puts us in the context of the class's "meta-class" (sometimes called the "eigenclass"). The meta-class is a special class that the class itself belongs to. However, at this point, you don't need to worry about it. All this construct does is allow us to define methods without the self. prefix.