You are previewing The Book of Ruby.

The Book of Ruby

Cover of The Book of Ruby by Huw Collingbourne Published by No Starch Press
  1. The Book of Ruby
    1. Acknowledgments
    2. Introduction
      1. What Is Ruby?
      2. What Is Rails?
      3. Matters of Ruby Style
      4. How to Read This Book
      5. Digging Deeper
      6. Making Sense of the Text
      7. Downloading Ruby
      8. Getting the Source Code of the Sample Programs
      9. Running Ruby Programs
      10. The Ruby Library Documentation
    3. 1. Strings, Numbers, Classes, and Objects
      1. Getting and Putting Input
      2. Strings and Embedded Evaluation
      3. Numbers
      4. Comments
      5. Testing a Condition: if..then
      6. Local and Global Variables
      7. Classes and Objects
    4. 2. Class Hierarchies, Attributes, and Class Variables
      1. Superclasses and Subclasses
      2. Passing Arguments to the Superclass
      3. Accessor Methods
      4. Attribute Readers and Writers
      5. Calling Methods of a Superclass
      6. Class Variables
    5. 3. Strings and Ranges
      1. User-Defined String Delimiters
      2. Backquotes
      3. String Handling
      4. Ranges
    6. 4. Arrays and Hashes
      1. Arrays
      2. Hashes
    7. 5. Loops and Iterators
      1. for Loops
      2. Blocks and Block Parameters
      3. Iterating upto and downto
      4. Multiple Iterator Arguments
      5. while Loops
      6. until Loops
      7. loop
    8. 6. Conditional Statements
      1. if..then..else
      2. and, or, and not
      3. Negation
      4. if..elsif
      5. unless
      6. if and unless Modifiers
      7. Case Statements
    9. 7. Methods
      1. Class Methods
      2. What Are Class Methods For?
      3. Class Variables
      4. Ruby Constructors: new or initialize?
      5. Singleton Methods
      6. Singleton Classes
      7. Overriding Methods
      8. Public, Protected, and Private Methods
    10. 8. Passing Arguments and Returning Values
      1. Summarizing Instance, Class, and Singleton Methods
      2. Returning Values
      3. Returning Multiple Values
      4. Default and Multiple Arguments
      5. Assignment and Parameter Passing
      6. Integers Are Special
      7. The One-Way-In, One-Way-Out Principle
      8. Modifying Receivers and Yielding New Objects
      9. Potential Side Effects of Reliance on Argument Values
      10. Parallel Assignment
    11. 9. Exception Handling
      1. rescue: Execute Code When Error Occurs
      2. ensure: Execute Code Whether or Not an Error Occurs
      3. else: Execute Code When No Error Occurs
      4. Error Numbers
      5. retry: Attempt to Execute Code Again After an Error
      6. raise: Reactivate a Handled Error
    12. 10. Blocks, Procs, and Lambdas
      1. What Is a Block?
      2. Line Breaks Are Significant
      3. Nameless Functions
      4. Look Familiar?
      5. Blocks and Arrays
      6. Procs and Lambdas
      7. Block or Hash?
      8. Creating Objects from Blocks
      9. What Is a Closure?
      10. yield
      11. Blocks Within Blocks
      12. Passing Named Proc Arguments
      13. Precedence Rules
      14. Blocks as Iterators
    13. 11. Symbols
      1. Symbols and Strings
      2. Symbols and Variables
      3. Why Use Symbols?
    14. 12. Modules and Mixins
      1. A Module Is Like a Class . . .
      2. Module Methods
      3. Modules as Namespaces
      4. Included Modules, or “Mixins”
      5. Name Conflicts
      6. Alias Methods
      7. Mix In with Care!
      8. Including Modules from Files
    15. 13. Files and IO
      1. Opening and Closing Files
      2. Characters and Compatibility
      3. Files and Directories
      4. Copying Files
      5. Directory Inquiries
      6. A Discursion into Recursion
      7. Sorting by Size
    16. 14. YAML
      1. Converting to YAML
      2. Nested Sequences
      3. Saving YAML Data
      4. Omitting Variables on Saving
      5. Multiple Documents, One File
      6. A YAML Database
      7. Adventures in YAML
    17. 15. Marshal
      1. Saving and Loading Data
      2. Omitting Variables on Saving
      3. Saving Singletons
    18. 16. Regular Expressions
      1. Making Matches
      2. Match Groups
      3. MatchData
      4. Prematch and Postmatch
      5. Greedy Matching
      6. String Methods
      7. File Operations
    19. 17. Threads
      1. Creating Threads
      2. Running Threads
      3. Going Native
      4. The Main Thread
      5. Thread Status
      6. Ensuring That a Thread Executes
      7. Thread Priorities
      8. The Main Thread Priority
      9. Mutexes
      10. Fibers
    20. 18. Debugging and Testing
      1. IRB: Interactive Ruby
      2. Debugging
      3. Unit Testing
    21. 19. Ruby on Rails
      1. Installing Rails
      2. Model-View-Controller
      3. A First Ruby on Rails Application
      4. Create a Rails Application
      5. Create a Controller
      6. Anatomy of a Simple Rails Application
      7. The Generate Controller Script Summarized
      8. Create a View
      9. Rails Tags
      10. Let’s Make a Blog!
    22. 20. Dynamic Programming
      1. Self-Modifying Programs
      2. eval
      3. Special Types of eval
      4. Adding Variables and Methods
      5. Creating Classes at Runtime
      6. Bindings
      7. send
      8. Removing Methods
      9. Handling Missing Methods
      10. Writing Programs at Runtime
      11. Exploring Further
    23. A. Documenting Ruby with RDoc
    24. B. Installing MySQL for Ruby on Rails
      1. Downloading MySQL
      2. Installing MySQL
      3. Configuring MySQL
      4. Can’t Find the Database?
    25. C. Further Reading
      1. Books
      2. Ebooks
      3. Websites
    26. D. Ruby and Rails Development Software
      1. IDEs and Editors
      2. Web Servers
      3. Databases
      4. Ruby Implementations
    27. Index
    28. About the Author
    29. Colophon
    30. E. Updates
O'Reilly logo

Class Variables

A few other interesting things are going on in this program. Right at the top of the Thing class you will see this:

@@num_things = 0

The two @ characters at the start of this variable name, @@num_things, define this to be a class variable. The variables we’ve used inside classes up to now have been instance variables, preceded by a single @, like @name. Whereas each new object (or instance) of a class assigns its own values to its own instance variables, all objects derived from a specific class share the same class variables. I have assigned 0 to the @@num_things variable to ensure that it has a meaningful value at the outset.

Here, the @@num_things class variable is used to keep a running total of the number of Thing objects in ...

The best content for your career. Discover unlimited learning on demand for around $1/day.