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



