Ruby Programming/Classes and objects

Ruby Classes
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:

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 Class
An instance of a class is an object that belongs to 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. Almost every class has a new method: arrays, hashes, whatever. (Some classes like Integers and Numerics do not have the  method.  These classes do not allow duplicates to exist among instantiated objects.) When you create your own classes, you'll use the new method to create instances.

Creating Classes
Classes represent a type of an object, such as a book, a whale, a grape, or chocolate. Everybody likes chocolate (may not be true), so let's make a chocolate class:

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 <tt>length</tt> method:

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

You can also call a method by using <tt>send</tt>

However, using <tt>send</tt> 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.

Note that the typical definition of a method in Ruby starts with the keyword def.

You can, however, also use define_method instead, and combine this with .send, to call arbitrary methods:

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

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

Note the three different constructions: <tt>ClassName.method_name</tt> and <tt>self.method_name</tt> are essentially the same - outside of a method definition in a class block, <tt>self</tt> refers to the class itself. The latter is preferred, as it makes changing the name of the class much easier. The last construction, <tt>class << self</tt>, 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 <tt>self.</tt> prefix.