Jump to content

Ruby Programming/Classes and objects

From Wikibooks, open books for an open world

← Writing methods | Exceptions →


Ruby Classes

[edit | edit source]

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 Class

[edit | edit source]

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 new 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

[edit | edit source]

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:

    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.

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:

  class Foo
    self.class_eval {
      define_method(:'hey there') { puts 'Hey there man!' }
    }
  end
  foo = Foo.new
  foo.send :'hey there' # => Hey there man!

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 Methods

[edit | edit source]

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.