9.2 What is an object?

What is an object?

Everything in Ruby is an object. All objects have an identity; they can also hold state and manifest behaviour by responding to messages. These messages are normally dispatched through method calls.

A string is an example of a Ruby object. Each string object has its own identity exposed through methods like object_id, == and class. It also has to store the value of the string and thus has state. Methods like split, upcase, downcase exposes the behavior of the object.

But what about a method? Is that an object too? Ruby provides an object representation for methods as well. method(method_name) returns a method object that holds method_name. We cover this in detail in the RubyMonk Metaprogramming: Code Inspection chapter.

Blocks, lambdas, Class - all of the them are objects. Every expression in Ruby evaluates to an object. These are all objects:

  • Class
  • Module
  • Method
  • Object.new
  • "a string"
  • 42
  • lambda { puts "This is a block of code. An object!"}
  • SomeUserDefinedClass.new

Object Identity

The Ruby VM keeps track of every object in memory through the object_id method which is present in all objects in Ruby. Different objects always have different ids.

Example Code:

Output Window

As you can see in the above example, every time you create the string "a", the object has a new object_id. This means two objects were created for representing "a".

Let us now look at a different aspect of an object's identity: its origin. The class definition from which an object was instantiated is an intrinsic part of its identity. The class method serves this purpose and is present in every object in Ruby. A few examples:

Example Code:

Output Window

Here Foo is a class definition object - this is where we define the behaviour of all the objects instantiated by the class Foo. What is the class of the class definition itself? Let us take a look at class Foo to find out:

Example Code:

Output Window

String, Array, Foo - these are all objects that hold class definitions. All such class definitions themselves belongs to the class Class. The class definition objects can instantiate objects of its class.

Here is an exercise that you can solve using the class method. I have a couple of dishes - Soup, IceCream and ChineseGreenBeans (yum!). Objects of these classes can be added to the DeliveryTray.The DeliveryTray has to keep track of the number of dish of each type and suggest how many dishes it needs to carry.

Output Window

You can check whether an object, say x is an instance of a particular class A by comparing x.class == A. The instance_of? method is a standard Ruby method that is a shorthand for this:

Example Code:

Output Window

As we can see, even though B inherits from A, Ruby does not consider objects of type B to be instances of A.

In the next lesson we'll go through the behaviour of objects in Ruby.

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2014 by Jasim A Basheer