2 Objects, methods, and local variables
Ruby programming is centered on objects, and this chapter introduces the idea that writing Ruby means creating objects, sending them messages, and defining methods that carry out those messages. A generic object can be created with Object.new, then extended with custom behavior by defining methods directly on it. The chapter emphasizes that objects are the main units of design and action in Ruby, while classes are introduced as a supporting concept that will be explored later.
The chapter then explains how method calls work, including arguments, return values, and Boolean-style methods. It distinguishes between parameters in a method definition and arguments passed at call time, shows how methods can accept required, optional, and variable numbers of arguments, and explains how Ruby assigns them. It also demonstrates that every method call returns a value, usually the last evaluated expression, and that Ruby provides tools like string interpolation, question-mark method names, and conditional logic to make object behavior expressive and readable.
Finally, the chapter turns to local variables and object references, showing that variables do not hold objects themselves but references to objects. This helps explain why changing an object through one variable affects all variables that point to it, while reassignment simply makes a variable refer to a new object. The chapter also explores how Ruby interprets bare identifiers as keywords, local variables, or method calls, and introduces useful introspective and defensive methods such as object_id, respond_to?, send, dup, clone, and freeze for understanding and controlling object behavior.
Anatomy of a method definition.
Argument assignment logic in action.
Summary
- Create a new object and define methods for it.
- The basics of the message-sending mechanism by which you send requests to objects for information or action.
- The process of mapping elements of a domain (even a modest one-entity domain like “a ticket to an event”) onto a system of objects so that those objects can store information and perform relevant tasks.
- Several of the important built-in methods that every Ruby object comes with: object_id, respond_to?, and send.
- Details of the syntax for method argument lists, including the use of required, optional, and default-valued arguments.
- How local variables and variable assignment work.
- Ruby’s use of references to objects and how references play out when multiple variables refer to the same object.
- The role of “real world” modeling in the process of thinking about object-oriented program design—and why it’s important not to overemphasize real-world parallels and analogies.
- The language offers lots of facilities for developing program structure. Creating objects one by one, as we’ve done in this chapter, is little more than the tip of the iceberg.
FAQ
What is the basic role of objects in Ruby?
Ruby programs revolve around objects: you create them, give them behaviors, and ask them to do things or provide information. Most actions in Ruby are carried out by sending messages to objects.What does it mean to send a message to an object?
Sending a message means using dot syntax to ask an object to perform a method, such asobj.talk. In practice, the message is usually the name of a method the object can respond to.What is the difference between a method definition parameter and a method call argument?
Parameters are the variables listed in a method definition, while arguments are the actual values supplied when the method is called. For example,c in def obj.c2f(c) is a parameter, and 100 in obj.c2f(100) is an argument.How does Ruby determine the return value of a method?
A method returns the value of its last evaluated expression. You can also use thereturn keyword to make the return value explicit or to exit early from a method.What is the difference between true, false, and nil in Ruby?
true and false are Boolean objects. In conditionals, almost everything is treated as true except false and nil, which behave as false.What is the purpose of respond_to??
respond_to? checks whether an object knows how to handle a given message or method name. It is commonly used before calling a method dynamically.What is the purpose of the send method?
send lets you call a method when the method name is stored in a variable or determined at runtime. It is powerful, but should be used carefully, especially with user input.How do required, optional, and variable-length arguments work in Ruby?
Required arguments must be provided. Optional arguments can have default values, and a starred parameter like*args collects any extra arguments into an array.How do local variables differ from object references?
In Ruby, local variables don’t store objects directly; they store references to objects. If two variables reference the same object, changing the object through one variable affects what you see through the other.How does Ruby decide whether a bare word is a keyword, local variable, or method call?
Ruby first checks whether it is a reserved keyword. If not, it may treat it as a local variable assignment, a method call with parentheses, or resolve it based on existing definitions. If it can’t resolve it, Ruby raises aNameError.
The Well-Grounded Rubyist, Fourth Edition ebook for free