chapter seven

7 Built-in essentials

 

This chapter covers

  • Literal object constructors
  • Syntactic sugar
  • “Dangerous” and/or destructive methods
  • The to_* family of conversion methods
  • Boolean states and objects, and nil
  • Object-comparison techniques
  • Runtime inspection of objects’ capabilities

Chapter 7 is a kind of next-generation Ruby literacy guide, a deeper and wider version of chapter 1. Like chapter 1, this chapter has two goals: making it possible to take a certain amount of material for granted in later chapters, where it will arise in various places to varying degrees; and presenting you with information about Ruby that’s important and usable in its own right. Throughout this chapter, you’ll explore the richness that lies in every Ruby object, as well as some of the syntactic and semantic subsystems that make the language so interesting and versatile.

The chapter moves through a number of topics, so it’s probably worth having a look in advance at what you’re going to see. Here’s a lightly annotated summary:

7.1 Ruby’s literal constructors

7.2 Recurrent syntactic sugar

7.2.1 Defining operators by defining methods

7.2.2 Customizing unary operators

7.3 Bang (!) methods and “danger”

7.3.1 Destructive (receiver-changing) effects as danger

7.3.2 Destructiveness and “danger” vary independently

7.4 Built-in and custom to_* (conversion) methods

7.4.1 String conversion: to_s and other methods defined on Object

7.4.2 Array conversion with to_a and the * operator

7.4.3 Numerical conversion with to_i and to_f

7.4.4 Role-playing to_* methods

7.5 Boolean states, Boolean objects, and nil

7.5.1 True and false as states

7.5.2 true and false as objects

7.5.3 The special object nil

7.6 Comparing two objects

7.6.1 Equality tests

7.6.2 Comparisons and the Comparable module

7.7 Inspecting object capabilities

7.7.1 Listing an object’s methods

7.7.2 Querying class and module objects

7.7.3 Filtered and selected method lists

7.8 Summary