Medium 9780596002145

Ruby in a Nutshell

Views: 1655
Ratings: (0)

Ruby is an absolutely pure object-oriented scripting language written in C and designed with Perl and Python capabilities in mind. While its roots are in Japan, Ruby is slowly but surely gaining ground in the US. The goal of Yukihiro Matsumoto, creator of Ruby and author of this book, is to incorporate the strengths of languages like Perl, Python, Lisp and Smalltalk. Ruby is a genuine attempt to combine the best of everything in the scripting world. Since 1993, Ruby mailing lists have been established, Web pages have formed, and a community has grown around it. The language itself is very good at text processing and is notable for its broad object orientation. Ruby is portable and runs under GNU/Linux (and other Unices) as well as DOS, MS Windows and Mac.With Ruby in a Nutshell, Matsumoto offers a practical reference to the features of this new language including the command-line options, syntax, built-in variables, functions, and many commonly used classes and modules. This guide covers the current stable version of Ruby (1.6), yet is applicable to the development version 1.7 and the next planned stable version 1.8. You will find a thorough description of Ruby's language syntax, and a description of the core functionality built into the standard Ruby interpreter, which has more than 800 built-in methods in 42 classes and modules.Ruby finds its power through its built-in libraries, and this handy volume take you through the many useful libraries that come with the standard Ruby distribution--from network access via HTTP and CGI programming, to data persistence using the DBM library. This book concludes with coverage of the unique tools that come with Ruby, including the debugger, profiler, and irb (or interactive ruby.)Find out how Ruby combines the strengths of other languages, and why it has captured the interest of so many open source programmers. As part of the successful "in a nutshell" series of books from O'Reilly & Associates, Ruby in a Nutshell is for readers who want a single desktop reference for all their needs.

List price: $19.99

Your Price: $15.99

You Save: 20%

 

23 Slices

Format Buy Remix

1. Introduction

ePub

Ruby has been readily adopted by programmers in Japan and has had much documentation written for it in Japanese. As programmers outside of Japan learn about the benefits of Ruby, there is a growing need for documentation in English. The first book I wrote for O'Reilly, Ruby Pocket Reference, was in Japanese. Since then Ruby has changed significantly. To meet the needs of non-Japanese programmers, we translated, updated, and expanded Ruby Pocket Reference into Ruby in a Nutshell.

Ruby is an object-oriented programming language that makes programming both enjoyable and fast. With the easy-to-use interpreter, familiar syntax, complete object-oriented functionality, and powerful class libraries, Ruby has become a language that can be applied to a broad range of fields from text processing and CGI scripts to professional, large-scale programs.

While Ruby is easy to learn, there are many details that you can't be expected to remember. This book presents those details in a clean and concise format. It is a reference to keep next to your desktop or laptop, designed to make Ruby even easier to use.

 

2.1. Command-Line Options

ePub

Ruby does what you'd expect it to do. It is highly consistent, and allows you to get down to work without having to worry about the language itself getting in your way.

Like most scripting language interpreters, Ruby is generally run from the command line. The interpreter can be invoked with the following options, which control the environment and behavior of the interpreter itself:

Used with -n or -p to split each line. Split output is stored in $F.

Checks syntax only, without executing program.

Changes directory before executing (equivalent to -X).

Enables debug mode (equivalent to -debug). Sets $DEBUG to true.

Specifies prog as the program from the command line. Specify multiple -e options for multiline programs.

Specifies pat as the default separator pattern ($;) used by split.

Displays an overview of command-line options (equivalent to -help).

Overwrites the file contents with program output. The original file is saved with the extension ext. If ext isn't specified, the original file is deleted.

 

2.2. Environment Variables

ePub

In addition to using arguments and options on the command line, the Ruby interpreter uses the following environment variables to control its behavior. The ENV object contains a list of current environment variables.

Search path for dynamically loaded modules.

Directory moved to when no argument is passed to Dir::chdir. Also used by File::expand_path to expand "~".

Directory moved to when no arguments are passed to Dir::chdir and environment variable HOME isn't set.

Search path for executing subprocesses and searching for Ruby programs with the -S option. Separate each path with a colon (semicolon in DOS and Windows).

Search path for libraries. Separate each path with a colon (semicolon in DOS and Windows).

Used to modify the RUBYLIB search path by replacing prefix of library path1 with path2 using the format path1;path2 or path1path2. For example, if RUBYLIB is:

and RUBYLIB_PREFIX is:

Ruby searches f:/ruby/site_ruby. Works only with DOS, Windows, and OS/2 versions.

Command-line options passed to Ruby interpreter. Ignored in taint mode (where $SAFE is greater than 0).

 

2.3. Lexical Conventions

ePub

Ruby programs are composed of elements already familiar to most programmers: lines, whitespace, comments, identifiers, reserved words, literals, etc. Particularly for those programmers coming from other scripting languages such as Perl, Python or tcl, you'll find Ruby's conventions familiar, or at least straightforward enough not to cause much trouble.

We'll leave the thorny questions like "How much whitespace makes code more readable and how much is distracting?" for another day. If you haven't already caught onto this theme, the Ruby interpreter will do pretty much what you expect with respect to whitespace in your code.

Whitespace characters such as spaces and tabs are generally ignored in Ruby code, except when they appear in strings. Sometimes, however, they are used to interpret ambiguous statements. Interpretations of this sort produce warnings when the -w option is enabled.

Interpreted as a+b (a is a local variable)

Interpreted as a(+b) (a, in this case, is a method call)

Ruby interprets semicolons and newline characters as the ending of a statement. However, if Ruby encounters operators, such as +, -, or backslash at the end of a line, they indicate the continuation of a statement.

 

2.4. Literals

ePub

I've often wondered why we programmers are so enamored with literals. I'm waiting for the day when a language comes along and introduces "figuratives." In the interim, the rules Ruby uses for literals are simple and intuitive, as you'll see the following sections.

Strings and numbers are the bread and butter of literals. Ruby provides support for both integers and floating-point numbers, using classes Fixnum, Bignum, and Float.

Integers are instances of class Fixnum or Bignum:

Floating-point numbers are instances of class Float:

A string is an array of bytes (octets) and an instance of class String:

Double-quoted strings allow substitution and backslash notation.

Single-quoted strings don't allow substitution and allow backslash notation only for \\ and \'.

Adjacent strings are concatenated at the same time Ruby parses the program.

#$var and #@var are abbreviated forms of #{$var} and #{@var}. Embeds value of expression in #{...} into a string.

In double-quoted strings, regular expression literals, and command output, backslash notation can be represent unprintable characters, as shown in Table 2-1.

 

2.5. Variables

ePub

 

2.6. Operators

ePub

Ruby supports a rich set of operators, as you'd expect from a modern language. However, in keeping with Ruby's object-oriented nature, most operators are in fact method calls. This flexibility allows you to change the semantics of these operators wherever it might make sense.

Most operators are actually method calls. For example, a + b is interpreted as a.+(b), where the + method in the object referred to by variable a is called with b as its argument.

For each operator (+ - * / % ** & | ^ << >> && ||), there is a corresponding form of abbreviated assignment operator (+= -= etc.)

Here are the operators shown in order of precedence (highest to lowest):

The following operators aren't methods and, therefore, can't be redefined:

Range operators function differently depending on whether or not they appear in conditionals, if expressions, and while loops.

In conditionals, they return true from the point right operand is true until left operand is true:

Evaluates expr2 immediately after expr1 turns true.

 

2.7. Methods

ePub

Methods are the workhorses of Ruby; all of your carefully crafted algorithms live in methods on objects (and classes). In Ruby, "method" means both the named operation (e.g. "dump") and the code that a specific class provides to perform an operation.

Strictly speaking, Ruby has no functions, by which I mean code not associated with any object. (In C++, this is what you might call a "global-scope function".) All code in Ruby is a method of some object. But Ruby allows you the flexibility of having some methods appear and work just like functions in other languages, even though behind the scenes they're still just methods.

Methods may be called with blocks of code specified that will be called from within the method.

Calls a method with blocks specified. The code in the block is executed after a value is passed from the method to the block and assigned to the variable (the block's argument) enclosed between ||.

A block introduces its own scope for new local variables. The local variables that appear first in the block are local to that block. The scope introduced by a block can refer local variables of outer scope; on the other hand, the scope introduced by class, module and def statement can't refer outer local variables.

 

2.8. Control Structures

ePub

 

2.9. Object-Oriented Programming

ePub

Phew, seems like a long time since I introduced Ruby as "the object-oriented scripting language," eh? But now you have everything you need to get the nitty-gritty details on how Ruby treats classes and objects. After you've mastered a few concepts and Ruby's syntax for dealing with objects, you may never want to go back to your old languages, so beware!

All Ruby data consists of objects that are instances of some class. Even a class itself is an object that is an instance of the Class class. As a general rule, new instances are created using the new method of a class, but there are some exceptions (such as the Fixnum class).

Class methods are defined with the def statement. The def statement adds a method to the innermost class or module definition surrounding the def statement. A def statement outside a class or module definition (at the top level) adds a method to the Object class itself, thus defining a method that can be referenced anywhere in the program.

When a method is called, Ruby searches for it in a number of places in the following order:

 

2.10. Security

ePub

Ruby is portable and can easily use code distributed across a network. This property gives you tremendous power and flexibility but introduces a commensurate burden: how do you use this capability without possibly causing damage?

Part of the answer lies in Ruby's security system, which allows you to "lock down" the Ruby environment when executing code that may be suspect. Ruby calls such data and code tainted. This feature introduces mechanisms that allow you to decide how and when potentially "dangerous" data or code can be used inside your Ruby scripts.

Ruby can execute programs with security checking turned on. The global variable $SAFE determines the level of the security check. The default safe level is 0, unless specified explicitly by the command-line option -T, or the Ruby script is run setuid or setgid.

$SAFE can be altered by assignment, but it isn't possible to lower the value of it:

$SAFE is thread local; in other words, the value of $SAFE in a thread may be changed without affecting the value in other threads. Using this feature, threads can be sandboxed for untrusted programs.

 

3.1. Predefined Variables

ePub

We will now explore the core functionality that is built into the standard Ruby interpreter. You will find descriptions of more than 800 built-in methods in 42 classes and modules. Topics covered include predefined variables, predefined global constants, and built-in functions.

Ruby's predefined (built-in) variables affect the behavior of the entire program, so their use in libraries isn't recommended. The values in most predefined variables can be accessed by alternative means.

The last exception object raised. The exception object can also be accessed using => in rescue clause.

The stack backtrace for the last exception raised. The stack backtrace information can retrieved by Exception#backtrace method of the last exception.

The input record separator (newline by default). gets, readline, etc., take their input record separator as optional argument.

The output record separator (nil by default).

The output separator between the arguments to print and Array#join (nil by default). You can specify separator explicitly to Array#join.

 

3.2. Predefined Global Constants

ePub

TRUE, FALSE, and NIL are backward-compatible. It's preferable to use true, false, and nil.

Synonym for true.

Synonym for false.

Synonym for nil.

An object providing access to virtual concatenation of files passed as command-line arguments or standard input if there are no command-line arguments. A synonym for $<.

An array containing the command-line arguments passed to the program. A synonym for $*.

An input stream for reading the lines of code following the __END__ directive. Not defined if __END__ isn't present in code.

A hash-like object containing the program's environment variables. ENV can be handled as a hash.

A string indicating the platform of the Ruby interpreter, e.g., i686-linux.

A string indicating the release date of the Ruby interpreter, e.g., 2001-09-19.

A string indicating the version of the Ruby interpreter, e.g., 1.6.5.

Standard error output stream. Default value of $stderr.

Standard input stream. Default value of $stdin.

Standard output stream. Default value of $stdout.

A Binding object at Ruby's top level.

 

3.3. Built-in Functions

ePub

Since the Kernel module is included by Object class, its methods are available everywhere in the Ruby program. They can be called without a receiver (functional form), therefore, they are often called functions.

Terminates program. If an exception is raised (i.e., $! isn't nil), its error message is displayed.

Returns obj after converting it to an array using to_ary or to_a.

Registers a block for execution when the program exits. Similar to END statement (referenced in Section 2.8), but END statement registers the block only once.

Registers a class classname to be loaded from file the first time it's used. classname may be a string or a symbol.

Returns the current variable and method bindings. The Binding object that is returned may be passed to the eval method as its second argument.

Returns true if the method was called with a block.

Passes a Continuation object c to the block and executes the block. callcc can be used for global exit or loop construct.

Returns the current execution stack in an array of the strings in the form file:line. If n is specified, returns stack entries from nth level on down.

 

3.4. Built-in Library

ePub

Ruby's built-in library provides you with a rich set of classes that form the foundation for your Ruby programs. There are classes for manipulating text (String), operating system services and abstractions (IO, File, Process, etc.), numbers (Integer, Fixnum, etc.), and so on.

Using these basic building blocks, you can build powerful Ruby programs. But wait, in the next chapter, I lay out the Standard Library, which extends Ruby's flexibility.

Ruby couldn't lay claim to being an "object-oriented scripting language" without providing fundamental tools for OOP. This basic support is provided through the Object class.

Death, taxes, and ... processing text. Yes, these are virtually inescapable in a programmer's life. In Ruby, I share your pain. Using the String, Regexp, and MatchData classes, Ruby provides sharp tools to slice, dice, and manipulate text to your heart's content.

One of the cornerstones of scripting languages is simple, flexible and powerful mechanisms for manipulating program data. In Ruby, the Array and Hash classes provide intuitive and rich capabilities for doing just that.

 

4. Standard Library Reference

ePub

We will now explore the useful libraries that come with the standard Ruby distribution, from network access via HTTP and CGI programming to data persistence using the DBM library.

The Ruby standard library extends the foundation of the Ruby built-in library with classes and abstractions for a variety of programming needs, including network programming, operating-system services, threads, and more. These classes provide flexible capabilities at a high level of abstraction, giving you the ability to create powerful Ruby scripts useful in a variety of problem domains.

Many common tasks are performed by Ruby programmers all over the world. Some of these tasks include network access such as TCP/IP and CGI, OS access, database access, controlling processes with threads, numeric calculations, implementing design classes, and manipulating dates. These are used so frequently that they are included with all standard distributions of Ruby; when you access these classes and methods from your programs, they will be available from the Standard Library. Could you write these libraries yourself? Probably. Would you feel confident they have been exhaustively tested, optimized, and debugged? Usually not. The Standard Library is a great time saver. And as Ruby grows and evolves, so will its Standard Library, to everyone's benefit.

 

5.1. Standard Tools

ePub

As a matter of course in Ruby, you edit your Ruby program and then feed it to the interpreter. Theoretically, the editor and interpreter are all you need to program Ruby. But you can get help from other tools. In this chapter, you will find descriptions of tools to help Ruby programmers.

The standard Ruby distribution contains useful tools along with the interpreter and standard libraries: debugger, profiler, irb (which is interactive ruby), and ruby-mode for Emacs. These tools help you debug and improve your Ruby programs.

It doesn't matter how easy a language is to use, it usually contains some bugs if it is more than a few lines long. To help deal with bugs, the standard distribution of Ruby includes a debugger. In order to start the Ruby debugger, load the debug library using the command-line option -r debug. The debugger stops before the first line of executable code and asks for the input of user commands.

Here are the debugger commands:

Sets breakpoints

Sets watchpoints

Displays breakpoints and watchpoints

 

5.2. Additional Tools

ePub

There are other useful tools that don't come bundled with the Ruby standard distribution. However, you do need to install them yourself.

ri is a online reference tool developed by Dave Thomas, the famous pragmatic programmer. When you have a question about the behavior of a certain method, e.g., IO#gets, you can invoke ri IO#gets to read the brief explanation of the method. You can get ri from http://www.pragmaticprogrammer.com/ruby/downloads/ri.html.

Here are the ri options:

Displays version and exits.

Sets the line length for the output (minimum is 30 characters).

Displays just a synopsis.

Uses the name module (default is Plain) for output formatting. Here are the available modules:

Simple tagged output

Default plain output

name should be specified in any of the following forms:

Class

Class::method

Class#method

Class.method

method

eRuby stands for embedded Ruby; it's a tool that embeds fragments of Ruby code in other files such as HTML files. Here's a sample eRuby file:

Here's the output from this sample file:

 

Load more


Details

Print Book
E-Books
Slices

Format name
ePub
Encrypted
No
Sku
9781449378806
Isbn
9781449378806
File size
0 Bytes
Printing
Not Allowed
Copying
Not Allowed
Read aloud
No
Format name
ePub
Encrypted
No
Printing
Allowed
Copying
Allowed
Read aloud
Allowed
Sku
In metadata
Isbn
In metadata
File size
In metadata