Một hệ luận đối với qui luật này là theo qui ước, các phương thức của Ruby -- ví dụ, biểu thức chính qui tìm kiếm — sẽ trả về các số, chuỗi, danh sách etc. nếu thành công, nhưng lại trả về nil nếu thất bại (ví dụ, không tìm thấy). | Organizing objects with classes In this chapter Creating multiple objects factory style with classes Setting and reading object state Automating creation of attribute read and write methods Class inheritance mechanics Syntax and semantics of Ruby constants 121 122 CHAPTER 5 Organizing objects with classes Creating a new object with and equipping that object with its own methods one method at a time is a great way to get a feel for the object-centeredness of Ruby programming. But this approach doesn t exactly scale if you re running an online box office and your database has to process records for tickets by the hundreds you ve got to find another way to create and manipulate ticket-like objects in your Ruby programs. Sure enough Ruby gives you a full suite of programming techniques for creating objects on a batch or factory basis. You don t have to define a separate price method for every ticket. Instead you can define a ticket class engineered in such a way that every individual ticket object automatically has the price method. Defining a class lets you group behaviors methods into convenient bundles so that you can quickly create many objects that behave essentially the same way. You can also add methods to individual objects if that s appropriate for what you re trying to do in your program. But you don t have to do that with every object if you model your domain into classes. Everything you handle in Ruby is an object and every object is an instance of some class. This fact holds true even where it might at first seem a little odd. For example when you manipulate an ActiveRecord object in a model file that object is an instance of a class Composer perhaps while at the same time the class itself is also an object. You ll learn in this chapter how this closely interwoven aspect of the design of Ruby operates. Classes and instances In most cases a class consists chiefly of a collection of method definitions. The class exists also in most cases for .