liveBook · Manning

Object

As explained in The Well-Grounded Rubyist, Fourth Edition

Instantiation is the process of creating an object as an instance of a class.

Create an object and call its method

class Ticket def event "Can't really be specified yet..." end end ticket = Ticket.new puts ticket.event Ticket.new creates a new Ticket object. The call to ticket.event runs an instance method defined in Ticket. section 3.1

An object is an instance of a class. A class usually groups methods that its instances can use; instantiation creates one of those instances. Object is itself a built-in class, so obj = Object.new is another complete example. The dot in Ticket.new sends new to the class, which is also an object. new constructs and returns the instance. section 3.1

The variable ticket refers to the new object; assignment generally binds a variable to an object reference rather than copying the object. You ask an object to act by sending it a message, such as event. Ruby matches that message to a method. Objects also have built-in methods such as object_id, respond_to?, and send. section 2.6 section 3.1

A class matters, but it does not permanently fix everything an object can do:

  • Create several instances of one class to share its instance methods. Getter and setter methods can expose state stored in each object's instance variables. section 3.9
  • Methods added later to a class or an ancestor can become available to existing objects. An individual object can also acquire its own method. section 3.8

Give one object a method

mag = Magazine.new def mag.wings puts "Look! I can fly!" end mag.wings The method is defined on mag individually, not as an instance method for every Magazine. Such per-object methods are called singleton methods. Most objects can acquire them, but integers, floats, and symbols are exceptions. section 3.8 section 13.1

Even familiar values participate in this object model: true and false are objects, each the sole instance of its respective class. The object nil represents the absence of a value or result. When behavior is in doubt, respond_to? can check whether an object handles a method; its class alone may not tell the whole story. section 7.5.2 section 3.8

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains construction, method calls, references, and changing behavior, while leaving detailed singleton-class mechanics beyond these examples.
Examples
Demonstrates Ticket.new and ticket.event, then adds a wings method to one Magazine instance.
What it assumes
Introduces objects through basic assignment and irb expressions before defining classes and instances.
What you can do after
Build it from scratch by defining a Ruby class, creating an instance with new, and calling its methods.
How it connects
Connects objects to classes, instance methods, object references, inheritance, and singleton methods.

Also explained in

Compare how 4 books explain object →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.