- •Programming Ruby The Pragmatic Programmer's Guide
- •Foreword
- •Preface
- •Ruby Sparkles
- •What Kind of Language Is Ruby?
- •Is Ruby for Me?
- •Why Did We Write This Book?
- •Ruby Versions
- •Installing Ruby
- •Building Ruby
- •Running Ruby
- •Interactive Ruby
- •Ruby Programs
- •Resources
- •Acknowledgments
- •Notation Conventions
- •Roadmap
- •Ruby.New
- •Ruby Is an Object-Oriented Language
- •Some Basic Ruby
- •Arrays and Hashes
- •Control Structures
- •Regular Expressions
- •Blocks and Iterators
- •Reading and 'Riting
- •Onward and Upward
- •Classes, Objects, and Variables
- •Inheritance and Messages
- •Inheritance and Mixins
- •Objects and Attributes
- •Writable Attributes
- •Virtual Attributes
- •Class Variables and Class Methods
- •Class Variables
- •Class Methods
- •Singletons and Other Constructors
- •Access Control
- •Specifying Access Control
- •Variables
- •Containers, Blocks, and Iterators
- •Containers
- •Implementing a SongList Container
- •Blocks and Iterators
- •Implementing Iterators
- •Blocks for Transactions
- •Blocks Can Be Closures
- •Standard Types
- •Numbers
- •Strings
- •Working with Strings
- •Ranges as Sequences
- •Ranges as Conditions
- •Ranges as Intervals
- •Regular Expressions
- •Patterns
- •Anchors
- •Character Classes
- •Repetition
- •Alternation
- •Grouping
- •Pattern-Based Substitution
- •Backslash Sequences in the Substitution
- •Object-Oriented Regular Expressions
- •More About Methods
- •Defining a Method
- •Variable-Length Argument Lists
- •Methods and Blocks
- •Calling a Method
- •Expanding Arrays in Method Calls
- •Making Blocks More Dynamic
- •Collecting Hash Arguments
- •Expressions
- •Operator Expressions
- •Miscellaneous Expressions
- •Command Expansion
- •Backquotes Are Soft
- •Assignment
- •Parallel Assignment
- •Nested Assignments
- •Other Forms of Assignment
- •Conditional Execution
- •Boolean Expressions
- •Defined?, And, Or, and Not
- •If and Unless Expressions
- •If and Unless Modifiers
- •Case Expressions
- •Iterators
- •Break, Redo, and Next
- •Variable Scope and Loops
- •Exceptions, Catch, and Throw
- •The Exception Class
- •Handling Exceptions
- •Tidying Up
- •Play It Again
- •Raising Exceptions
- •Adding Information to Exceptions
- •Catch and Throw
- •Modules
- •Namespaces
- •Instance Variables in Mixins
- •Iterators and the Enumerable Module
- •Including Other Files
- •Basic Input and Output
- •What Is an io Object?
- •Opening and Closing Files
- •Reading and Writing Files
- •Iterators for Reading
- •Writing to Files
- •Talking to Networks
- •Threads and Processes
- •Multithreading
- •Creating Ruby Threads
- •Manipulating Threads
- •Thread Variables
- •Threads and Exceptions
- •Controlling the Thread Scheduler
- •Mutual Exclusion
- •The Mutex Class
- •Condition Variables
- •Running Multiple Processes
- •Spawning New Processes
- •Independent Children
- •Blocks and Subprocesses
- •When Trouble Strikes
- •Ruby Debugger
- •Interactive Ruby
- •Editor Support
- •But It Doesn't Work!
- •But It's Too Slow!
- •Create Locals Outside Blocks
- •Use the Profiler
- •Ruby and Its World
- •Command-Line Arguments
- •Command-Line Options
- •Program Termination
- •Environment Variables
- •Writing to Environment Variables
- •Where Ruby Finds Its Modules
- •Build Environment
- •Ruby and the Web
- •Writing cgi Scripts
- •Using cgi.Rb
- •Quoting
- •Creating Forms and html
- •Cookies
- •Sessions
- •Embedding Ruby in html
- •Using eruby
- •Installing eruby in Apache
- •Improving Performance
- •Ruby Tk
- •Simple Tk Application
- •Widgets
- •Setting Widget Options
- •Getting Widget Data
- •Setting/Getting Options Dynamically
- •Sample Application
- •Binding Events
- •Scrolling
- •Just One More Thing
- •Translating from Perl/Tk Documentation
- •Object Creation
- •Running Ruby Under Windows
- •Windows Automation
- •Getting and Setting Properties
- •Named Arguments
- •For each
- •An Example
- •Optimizing
- •Extending Ruby
- •Ruby Objects in c
- •Value as a Pointer
- •Value as an Immediate Object
- •Writing Ruby in c
- •Evaluating Ruby Expressions in c
- •Sharing Data Between Ruby and c
- •Directly Sharing Variables
- •Wrapping c Structures
- •An Example
- •Memory Allocation
- •Creating an Extension
- •Creating a Makefile with extconf.Rb
- •Static Linking
- •Embedding a Ruby Interpreter
- •Bridging Ruby to Other Languages
- •Ruby c Language api
- •The Ruby Language
- •Source Layout
- •Begin and end Blocks
- •General Delimited Input
- •The Basic Types
- •Integer and Floating Point Numbers
- •Strings
- •Requirements for a Hash Key
- •Symbols
- •Regular Expressions
- •Regular Expression Options
- •Regular Expression Patterns
- •Substitutions
- •Extensions
- •Variable/Method Ambiguity
- •Variables and Constants
- •Scope of Constants and Variables
- •Predefined Variables
- •Exception Information
- •Pattern Matching Variables
- •Input/Output Variables
- •Execution Environment Variables
- •Standard Objects
- •Global Constants
- •Expressions Single Terms
- •Operator Expressions
- •More on Assignment
- •Parallel Assignment
- •Block Expressions
- •Boolean Expressions
- •Truth Values
- •And, Or, Not, and Defined?
- •Comparison Operators
- •Ranges in Boolean Expressions
- •Regular Expressions in Boolean Expressions
- •While and Until Modifiers
- •Break, Redo, Next, and Retry
- •Method Definition
- •Method Arguments
- •Invoking a Method
- •Class Definition
- •Creating Objects from Classes
- •Class Attribute Declarations
- •Module Definitions
- •Mixins---Including Modules
- •Module Functions
- •Access Control
- •Blocks, Closures, and Proc Objects
- •Proc Objects
- •Exceptions
- •Raising Exceptions
- •Handling Exceptions
- •Retrying a Block
- •Catch and Throw
- •Classes and Objects
- •How Classes and Objects Interact
- •Your Basic, Everyday Object
- •Object-Specific Classes
- •Mixin Modules
- •Extending Objects
- •Class and Module Definitions
- •Class Names Are Constants
- •Inheritance and Visibility
- •Freezing Objects
- •Locking Ruby in the Safe
- •Safe Levels
- •Tainted Objects
- •Reflection, ObjectSpace, and Distributed Ruby
- •Looking at Objects
- •Looking Inside Objects
- •Looking at Classes
- •Looking Inside Classes
- •Calling Methods Dynamically
- •Performance Considerations
- •System Hooks
- •Runtime Callbacks
- •Tracing Your Program's Execution
- •How Did We Get Here?
- •Marshaling and Distributed Ruby
- •Custom Serialization Strategy
- •Distributed Ruby
- •Compile Time? Runtime? Anytime!
- •Standard Library
Predefined Variables
Predefined Variables
The following variables are predefined in the Ruby interpreter. In these descriptions, the notation {} indicates that the variables are read-only; an error will be raised if a program attempts to modify a read-only variable. After all, you probably don't want to change the meaning of truehalfway through your program (except perhaps if you're a politician). Entries marked {} are thread local.
Many global variables look something like Snoopy swearing: $_,$!,$&, and so on. This is for ``historical'' reasons, as most of these variable names come from Perl. If you find memorizing all this punctuation difficult, you might want to have a look at the library file called ``English,'' documented on page 445, which gives the commonly used global variables more descriptive names.
In the tables of variables and constants that follow, we show the variable name, the type of the referenced object, and a description.
Exception Information
$! |
Exception |
The exception object passed to raise. {} |
$@ |
Array |
The stack backtrace generated by the last exception. See Kernel#caller on page 413 for details. {} |
Pattern Matching Variables
These variables (except $=) are set tonilafter an unsuccessful pattern match.
$& |
String |
The string matched by the last successful pattern match. This variable is local to the current scope. {} |
$+ |
String |
The contents of the highest-numbered group matched in the last successful pattern match. Thus, in "cat" =~/(c|a)(t|z)/,$+will be set to ``t''. This variable is local to the current scope. {} |
$` |
String |
The string preceding the match in the last successful pattern match. This variable is local to the current scope. {} |
$' |
String |
The string following the match in the last successful pattern match. This variable is local to the current scope. {} |
$= |
Object |
If set to any value apart from nilorfalse, all pattern matches will be case insensitive, string comparisons will ignore case, and string hash values will be case insensitive. |
$1 to $9 |
String |
The contents of successive groups matched in the last successful pattern match. In "cat" =~/(c|a)(t|z)/,$1will be set to ``a'' and$2to ``t''. This variable is local to the current scope. {} |
$~ |
MatchData |
An object that encapsulates the results of a successful pattern match. The variables $&,$`,$', and$1to$9are all derived from$~. Assigning to$~changes the values of these derived variables. This variable is local to the current scope. {} |
Input/Output Variables
$/ |
String |
The input record separator (newline by default). This is the value that routines such as Kernel#gets use to determine record boundaries. If set tonil,getswill read the entire file. |
$-0 |
String |
Synonym for $/. |
$\ |
String |
The string appended to the output of every call to methods such as Kernel#print andIO#write . The default value isnil. |
$, |
String |
The separator string output between the parameters to methods such as Kernel#print andArray#join . Defaults tonil, which adds no text. |
$. |
Fixnum |
The number of the last line read from the current input file. |
$; |
String |
The default separator pattern used by String#split . May be set from the command line using the-Fflag. |
$< |
Object |
An object that provides access to the concatenation of the contents of all the files given as command-line arguments, or $stdin(in the case where there are no arguments).$<supports methods similar to aFileobject:binmode,close,closed?,each,each_byte,each_line,eof,eof?,file,filename,fileno,getc,gets,lineno,lineno=,pos,pos=,read,readchar,readline,readlines,rewind,seek,skip,tell,to_a,to_i,to_io,to_s, along with the methods inEnumerable. The methodfilereturns aFileobject for the file currently being read. This may change as$<reads through the files on the command line. {} |
$> |
IO |
The destination of output for Kernel#print andKernel#printf . The default value is$stdout. |
$_ |
String |
The last line read by Kernel#gets orKernel#readline . Many string-related functions in theKernelmodule operate on$_by default. The variable is local to the current scope. {} |
$defout |
IO |
Synonym for $>. |
$-F |
String |
Synonym for $;. |
$stderr |
IO |
The current standard error output. |
$stdin |
IO |
The current standard input. |
$stdout |
IO |
The current standard output. |