← Writing methods| Exceptions →
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'sclass method. For example, try this:
puts"This is a string".classputs9.classputs["this","is","an","array"].classputs({:this=>"is",:a=>"hash"}).classputs: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.
An instance of a class is an object thatbelongs 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 thenew method. For example,my_string = "" is the same asmy_string = String.new. Almost every class has anew method: arrays, hashes, whatever. (Some classes like Integers and Numerics do not have thenew method. These classes do not allow duplicates to exist among instantiated objects.) When you create your own classes, you'll use thenew method to create instances.
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:
classChocolatedefeatputs"That tasted great!"endend
Let's take a look at this. Classes are created via theclass 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 thelength method:
# outputs "5"puts"hello".length
To call theeat method of an instance of the Chocolate class, we would use this code:
my_chocolate=Chocolate.newmy_chocolate.eat# outputs "That tasted great!"
You can also call a method by usingsend
"hello".send(:length)# outputs "5"my_chocolate.send(:eat)# outputs "That tasted great!"
However, usingsend 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 keyworddef.
You can, however, also usedefine_method() instead, and combine this with.send, to call arbitrary methods:
classFooself.class_eval{define_method(:'hey there'){puts'Hey there man!'}}endfoo=Foo.newfoo.send:'hey there'# => Hey there man!
Inside a method of a class, the pseudo-variableself (a pseudo-variable is one that cannot be changed) refers to the current instance. For example:
classIntegerdefmorereturnself+1endend3.more# -> 47.more# -> 8
You can also create methods that are called on a class rather than an instance. For example:
classStrawberrydefStrawberry.colorreturn"red"enddefself.sizereturn"kinda small"endclass<<selfdefshapereturn"strawberry-ish"endendendStrawberry.color# -> "red"Strawberry.size# -> "kinda small"Strawberry.shape# -> "strawberry-ish"
Note the three different constructions:ClassName.method_name andself.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 theself. prefix.