Index

Symbols

″ (double quotes), use with string literals, 44–45

′ (single quotes), use with string literals, 44–45

- (subtraction) operator

as binary or unary operator, 132

overloading, 131

. (period)

for matching any single character, 54

in module syntax, 185

using asterisk (*) in conjunction with, 58

/ (division) operator, 131

/ (forward slashes), in regular expression syntax, 58–59

: (colon), in symbol syntax, 66–67

:: (double-colon), in module syntax, 185

; (semicolon), for separating statements in Ruby code, 10–11

(backslash)

escaping special meanings of punctuation characters in regular expressions, 54

escaping strings, 44–45

| (or) operator, 131

| (vertical bar), in syntax of alternatives in regular expressions, 56–57

||= operator, in expression-based initialization, 26–27

+ (addition) operator

as binary or unary operator, 132

non-commutative nature of, 137

overloading, 131

when to use, 136–137

=~ operator, testing if regular expression matches a string, 59–60

== (double-equals) operator

broadening the scope of, 145–146

numeric classes accepting Float as equals, 154–156

overview of, 143–144

RSpec and, 138

symmetry principal and, 146–147

transitive property of, 147–149

=== (triple equals) operator, for case statements, 23, 149–150

=> (hash rocket), 30

! method names ending with, 48

! unary operator, 131–132

#, in comment syntax, 6

$, as string delimiter, 45

% (formatting operator), strings, 137–138

% (modulo) operator, 131, 152

%q, for arbitrarily quoted strings, 45–46

& (and) operator, 131

( ) (parentheses)

readability and, 12

Ruby conventions for calling defining/methods, 9–10

* (asterisk)

in method definition with extra arguments, 31–32

in regular expressions, 57–58

* (multiplication) operator, 131

? (question mark), using with regular expressions, 62–63

?: (ternary operator), in expression-based decision making, 26

@@, in class variable syntax, 169

[ ] (square brackets)

adding to indexing-related class, 135

operator-like syntax and, 133

as string delimiter, 45

using with regular expressions, 55

[ ]=

adding to indexing-related class, 135

operator-like syntax and, 133

^ (exclusive or) operator, 131

{} (braces), in code block syntax, 11

<< (left shift operator), 131, 135

<=> operator

Float and Fixnum classes and, 154–156

sort method and, 214

A

accessor methods, using with class variables, 170

ActiveRecord

callbacks and, 177

composed method approach and, 127–128

as database interface library, 335

DataMapper compared with. see DataMapper

example of delegation, 282–283

example of execute around, 230

example of saved code blocks, 243

examples of internal DSLs, 346

find method, 66–67

magic methods, 291–292

silence method, 231

add_unique_word method, 120

addition (+) operator. see + (addition) operator

alias_method, for renaming methods, 297–299

alternatives, in regular expressions, 55–57

ancestors method, for viewing inheritance ancestry, 199

and (&) operator, 131

APIs

avoiding trouble when using method_missing for, 289–290

building form letters one word at a time, 286–288

building with method_missing, 292

examples of use of method_missing for, 290–292

review of applying method_missing to, 292

supported by strings, 47–49

transition from API to DSL, 341–344

user focus in creating easy-to-use APIs, 289

when to use instead of internal DSLs, 348

archives, gems and, 370

arguments

code blocks taking, 208

execute around methods taking, 226–227

methods taking fixed or variable numbers of, 30–31

naming conventions, 8

singleton methods accepting, 159

arrays

APIs for, 35

caution when iterating over, 40–41

each method, 34, 217

improper use of, 41

method-passing with, 30–32

monkey patching for adding methods to, 302

order of, 38

overview of, 29

public methods for array instances, 36

reverse method, 36–37

shortcuts for accessing, 30

sort method, 37

assert method, Test::Unit, 98

assert_equal method, Test::Unit, 98

assert_match method, Test::Unit, 101

assert_nil method, Test::Unit, 101

assert_not_equal method, Test::Unit, 101

assert_not_nil method, Test::Unit, 101

assertions, in Test::Unit, 101

asterisk (*)

in method definition with extra arguments, 31–32

in regular expressions, 57–58

asymmetrical equality relationships, 147

at_exit hook

informing when time is up, 255–256

in Test::Unit, 259–260

attr_accessor

accessing class instance variables, 176

in default set of methods in Object class, 82

as subclass-changing method, 327–328

attr_reader, as subclass-changing method, 327

attr_writer, as subclass-changing method, 327–328

attributes, at class level, 176–177

automating

gem creation, 375–377

testing gems, 94

B

backslash ()

escaping special meanings of punctuation characters, 54

escaping strings, 44–45

BasicObject, use in delegation with method_missing, 280–281

Bignum class, 154–156

binary operators

operating across classes, 134–135

overview of, 131–132

bitwise operators, 131

blank? method, adding to String class, 301

block_given? passing code blocks in methods, 208, 233

blocks. see code blocks

boolean logic

false and true values in Ruby, 23–25

mapping boolean operators to union and intersection operations, 135

braces ({}), in code block syntax, 11

break, in code blocks, 216

bugs, 94. see also tests

bytes, strings as collections of, 49–50

C

C language, 382

C# language, 336

call backs

ActiveRecord objects and, 177

creating listeners for, 234–236

using explicit code blocks for, 236–237

call method

calling code blocks explicitly, 234

Proc.new and, 241

camel case, class naming conventions, 8

Capistrano, 243–244

Cardinal, 382

case sensitivity, working with strings, 47

case statements

example of use of, 21–23

triple equals operator (===) for, 149–150

characters

matching any one of a bunch of characters, 55

matching one character at a time, 54–55

strings as collections of, 49

chomp method, working with strings, 47

chop method, working with strings, 47

clarity

of code, 94

qualities of good code, 4

class definitions, executable. see classes, self modifying

class instance variables

avoiding trouble when using, 179

examples of use of, 177–179

for holding onto classwide values, 174–175

review of, 179

singleton class used to add convenience to, 176–177

subclasses and, 175–176

class methods. see also singleton methods

adding convenience to class instance variables, 176–177

avoiding trouble when using, 165–166

for building instance methods, 321–323

defining, 163–164

extending modules and, 197–198

handling missing constants. see const_missing

included hook used with, 254–255

making structural changes to classes, 309–310

overview of, 162

uses of, 164–165

class variables

avoiding trouble when using, 179

example of use of, 170

problems associated with global nature of, 171–174

review of, 179

storing class level data with, 169

tendency to wander from class to class, 171

URI class and, 177–178

class_eval, for creating methods, 322–323, 329

classes

accessing in modules, 182–183

adding iterator methods to, 210–211

avoiding name collisions, 377–378

benefits of dynamic typing, 85, 89

binary operators used across, 134–135

as both factory and container, 182

changing class definition, 305–308

class/instance approximation in defining methods, 157

composed method for building, 122–123

as container for methods, 74

defining, 294

do anything to any class, anytime, 297–299

as factory for creating instances, 74–75

fixing broken, 295–296

flexibility resulting from decoupling, 90–91

holding onto classwide values, 174–175

hook for informing when a class gains a subclass, 250–253, 257–259

hook for informing when a module gets included in a class, 253–255

mixins for sharing code between unrelated classes, 195–197

modifying, 295–297

modules for grouping related, 182

modules for organizing into hierarchies, 181

modules for swapping groups of related classes at runtime, 186–187

naming conventions, 8

open nature in Ruby. see open classes

preference for bare collections over specialized classes, 38–40

renaming methods using alias_method, 297–299

storing class level data, 169

superclasses, 75–76

when to use modules vs. naked classes, 189

writing methods for. see methods, writing

classes, self modifying

adding programming logic to classes, 308–309

avoiding trouble when using, 314–315

class methods that change class, 309–310

defining classes and, 305–308

examples of use of, 310–313

overview of, 305

review of, 315–316

classes, that modify subclasses

avoiding trouble when using, 330–332

class methods that build instance methods, 321–323

define_method for creating methods, 324

difficulty of subclassing and, 319–321

example of paragraph subclass of document class, 317–319

examples of use of, 327–329

no limits on modifying subclasses from superclass methods, 324–326

overview of, 317

review of, 332

closure (scope)

avoiding trouble when using, 241–242

code blocks drag scope along to wherever they are applied, 225–227

code

clarity and conciseness of, 94

concise vs. cryptic, 94

dynamic typing increasing compactness of, 85–89

embedding in classes, 308

format of. see code format

less code, less likelihood of error, 84

qualities of good code, 4

readability of, 12–13

sharing between unrelated classes, 195

code blocks

at_exit hook, 255–256

multiline vs. single line, 12

Ruby conventions, 11

code blocks, as iterators

adding multiple iterators, 210–211

adding single iterator, 209–210

avoiding trouble when using, 215–216

creating by tacking on to the end of method calls, 207–208

Enumerable module and, 213–215

overview of, 207

returning values, 208–209

review of, 218

spectrum of iterator types, 217–218

taking arguments, 208

writing iterators for collections that do not yet exist, 211–213

code blocks, saving for later use

applying to call backs, 234–237

applying to lazy initialization, 237–239

avoiding trouble when using, 240–242

examples of use of, 243–244

explicit vs. implicit approaches to passing blocks, 233–234

overview of, 233

producing instant block objects, 239–240

review of, 244–245

code blocks, using execute around

applying to logging, 222–224

applying to object initialization, 225, 229–230

avoiding trouble when using, 228–229

delivering code where needed, 219

dragging scope along to wherever they are applied, 225–227, 241–242

for functions that must happen before or after operations, 224

returning something from, 227–228

silence method for turning logging off, 231

code format

breaking rules and, 14–15

code blocks, 11

indentation, 5–6

naming conventions, 8–9

“one statement per line” convention, 10–11

parentheses in calling/defining methods, 9–10

qualities of good code, 4

readability and, 12–13

review of conventions, 15

collections

adding left shift operator to collection class, 135

caution when iterating over, 40–41

collection-related methods in Enumerable class, 213

improper use of arrays and hashes, 41–42

iterating through, 33–36

knowing which methods change and which leave as is, 36–38

method calls for accessing, 30–33

order of hashes, 38

overview of, 29

preference for bare collections over specialized classes, 38–40

review of, 42

shortcuts for accessing, 29–30

colon (:), in symbol syntax, 66–67

comments

dynamic typing and, 93

example in set.rb class, 13–14

when and how often to use, 6–8

comparison operator, 23

complexity, simplicity as solution to, 92

composed method

ActiveRecord::Base class example, 127–128

applying to TextCompressor class, 121

for building classes, 122–123

characteristics of, 121–122

compression algorithm, 117–118

conciseness, of code, 4, 94

conditions, syntax in control statements, 10

consistency, of Ruby object system, 76–77

const_missing

avoiding trouble when using, 270–271

examples of use of, 269–270

handling missing constants, 267–268

review of, 271

constants

accessing in modules, 183

handling missing. see const_missing

modules for organizing into hierarchies, 181–182

modules for swapping groups of related constants at runtime, 186–187

naming conventions, 8–9

stashing in mixins, 204–205

containers

modules as, 181–182

treating modules as object rather than static containers, 186

control structures

||= in expression-based initialization, 26–27

boolean logic and, 23–25

case statement, 21–23

code capturing values of while or if statements, 25

each method preferred over for loops, 20–21

if, unless, while, and until statements, 17–19

modifier forms, 19–20

overview of, 17

review of, 27

syntax for conditions in, 10

ternary operator (?:) in decision making, 26

Cucumber testing tool, 363–364

D

data

storing class level, 169, 174

using strings for processing, 66–67

data types

built-in, 58–60

disadvantages of adding type checking code, 91

dynamic. see dynamic typing

static. see static typing

type documentation, 92

DataMapper

example of use of modules in, 190–191

mixins used by, 202–203

debugging, logging for, 219

decomposing classes

into small methods, 123

troubles arising from, 126–127

decoupling, with dynamic typing, 89–92

def

class methods that build instance methods, 322

last def principle, 295

define_method, for creating methods, 324, 328

defined? boolean logic and, 24

delegate.rb file, 281–282

delegation

avoiding trouble when using, 279–281

example of use by ActiveRecord, 282–283

method_missing applied to, 277–278

overview of, 273

problems with traditional style of, 275

pros/cons of, 274–275

review of, 283

selective approach to, 278–279

SimpleDelegator class, 281–282

delete method, for arrays, 37

Dir class, 217

directories

generating directory structure of gems, 377

organizing for gems packaging, 370–372

division (/) operator, 131

DLL Hell, 370

do keyword, in code block syntax, 11

documentation

compensating for lost documentation due to required type declarations, 92–93

Ruby implementations, 389

DocumentIdentifier class, 142

documents

compressing specification documents, 117–118

creating identifier, 142

handling document errors, 266–267

lazy documents, 86–89

paragraph subclass of document class, 317–319

Ruby coding conventions illustrated in Document class, 5

Domain Specific Languages, external. see DSLs (Domain Specific Languages), external

Domain Specific Languages, internal. see DSLs (Domain Specific Languages), internal

double quotes (″), use with string literals, 44–45

double-colon (::), in module syntax, 185

double-equals (==) operator. see == (double-equals) operator

downcase method, working with strings, 47

DSLs (Domain Specific Languages), external

avoiding trouble when using, 360–362

building parser for XML processing language, 353–356

examples of use of, 362–364

overview of, 336, 351–352

regular expressions for parsing, 356–358

review of, 364–365

Treetop parsing tool, 358–360

when to use as alternative to internal DSL, 352

DSLs (Domain Specific Languages), internal

avoiding trouble when using, 347–349

based on Ruby code, 352

dealing with XML, 336–341

examples of use of, 345–346

method_missing used with, 344

narrow focus of, 336

overview of, 335

review of, 349

transition from API to DSL, 341–344

when to use as alternative to external DSL, 352

duck typing, 88–89

dynamic typing

compactness of code and, 85–89

comparing File and StringIO classes, 94–95

compensating for lost documentation due to required type declarations, 92–93

extreme decoupling with, 89–92

overview of, 85

Set class and, 95–96

trade offs in use of, 93–94

E

each method

adding iterator methods to classes, 210–212

avoiding trouble when iterating arrays, 40

iteration with, 34

preferred over for loops, 20–21

types of iterators and, 217

each_address method, Resolv class, 217

each_cons method, Enumerable module and, 213–214

each_object method, ObjectSpace class, 217–218

each_splice method, Enumerable module and, 214

eigenclasses. see singleton classes

encryption

managing with class methods, 309–310

managing with programming logic in classes, 308–309

end keyword, in code block syntax, 11

Enumerable module, 213–215

Enumerator class, 214

eql? method

Hash class using, 152–153

overview of, 150–152

restrictive view of equality in, 153

equal? method, for testing object identity, 143

equality

avoiding trouble when using, 153–154

broadening the scope of double-equals operator, 145–146

double-equals (==) operator, 143–144

eql? method, 150–153

equal? method, 143

Float and Fixnum classes and <=> operator, 154–156

identifiers and, 142

methods for, 142–143

overview of, 141

review of, 154–156

symbols and, 67–68

symmetry principal and, 146–147

transitive property and, 147–149

triple equals operator (===), 149–150

ERB, 362–363

eval method, Object class, 78

exception handling. see also method_missing, error handling with

with execute around, 228

handling document errors, 266–267

internal DSLs and, 347

logging and, 222, 224

exclusive or (^) operator, 131

executable class definitions. see classes, self modifying

execute around

avoiding trouble when using, 228–229

for functions that must happen before or after operations, 224

initializing objects with, 225, 229–230

passing arguments and, 226–227

returning something from code blocks, 227–228

external DSLs. see DSLs (Domain Specific Languages), external

F

false

in boolean logic, 23–24

false as an object, 76

File class, comparing with StringIO class, 94–95

filenames, avoiding name collisions, 378

find method, ActiveRecord, 66–67

find_index, map method compared with, 35

Fixnum class, 154–156

Float class, 154–156

floating point numbers, 296

for loops, 20–21

formatting operator (%), for strings, 137–138

forward slashes (/), in regular expression syntax, 58–59

forwardable.rb, 328–329

Fowler, Martin, 336

G

gem files, 370

gem install command, 374

gem list command, 368–369

Gemcutter, adding gems to Gemcutter repository, 375–376

gems

automating creation of, 375–376

avoiding trouble when using, 377–380

building, 370–374

creating, 378–379

examples of use of, 376–377

installing and consuming, 367–368

nuts and bolts of, 369–370

packaging programs as, 367

review of, 380

shoulda gem, 108

uploading to repository, 374–375

versioning support, 368–369

gemspec file, 373–374

GEM::Specification instances, 229–230

gets method, Object class, 78

global variables, class variables compared with, 174

gsub

inflection rules based on, 50–51

passing regular expressions into, 60

H

HAML, 361

hash rocket (=>), 30

hashes

APIs for, 35

caution when iterating over, 40–41

each method, 34, 217

Hash class, 69

hash tables and eql? method, 150–153

hash values, 152

improper use of, 41–42

method-passing with, 33

order of, 38

overview of, 29

public methods, 36

shortcut for accessing, 30

symbols as hash keys, 68–71

HashWithIndifferenceAccess class, 71

helper methods, Rails, 203–204

hoe, for automating creation of gems, 376–377

hooks

avoiding trouble when using, 257–259

examples of use of, 259–260

informing when a class gains a subclass, 250–253

informing when a module gets included in a class, 253–255

informing when time is up, 255–256

method_missing. see method_missing

overview of, 249

review of, 261

set_trace_func, 256–257

value of, 332

HTML

HAML for HTML templating, 361

Rails helper methods for creating, 203–204

I

identifiers

creating document identifier, 142

testing object identity, 143

if statements

case statement compared with, 23

code capturing values of, 25

example of use of, 17–18

modifier forms of, 20

included method, informing when a module gets included in a class, 253–255

indentation, Ruby conventions, 5–6

indexing strings, 52

inflection rules, for strings, 50–51

inheritance

ancestors method, 199

class variables searching for associated classes, 171, 173

mixin modules and, 201–202

superclasses in inheritance tree, 193

inherited method

avoiding trouble when using, 257–259

hook for informing when a class gains a subclass, 250–253

initialization

defining classes, 294

of objects using execute around, 225

saved code blocks used for lazy initialization, 237–239

of variables, 26

initialize method, for defining classes, 294

inject method, collection methods, 35–36

instance _of?, 145

instance methods

class methods that build, 321–323

instance_methods method, 307

instance.method_name, 74

instance variables

attaching to class objects, 174

instance_variables method, 79

naming conventions, 8

instances

classes as factory for creating, 74–75

class/instance approximation in defining methods, 157

inheriting methods of Object class, 78

singleton methods defined for single object instance, 158–159

integers, 154–156

interfaces, 285. see also APIs

internal DSLs. see DSLs (Domain Specific Languages), internal

intersection operations, mapping boolean operators to, 135

IronRuby implementation, 382

iteration

adding an iterator, 209–210

adding multiple iterators, 210–211

avoiding trouble when using, 215–216

caution when iterating over arrays and hashes, 40–41

code blocks used as iterators, 207

Enumerable module and, 213–215

spectrum of iterator types, 217–218

through collections, 33–36

writing iterators for collections that do not yet exist, 211–213

J

JAR file Hell, 370

Java

examples of general purpose languages, 336

JRuby and, 387

Java Virtual Machine (JVM), 387

JRuby

overview of, 382, 387–388

support and documentation, 389

JVM (Java Virtual Machine), 387

K

kind_of? method

double-equals (==) operator and, 146

locating modules in classes with, 199

L

lambda method, creating default Proc object using, 239–241

lazy initialization, 237–239

“leave it to the last minute” technique, 88

left shift operator (<<), 131, 135

lib directory

organizing for gems packaging, 372

sow command generating, 377

lines, strings as collections of, 50

listeners, for call backs, 234–236

literals, shortcuts for accessing collections, 29–30

load methods, managing logging with, 221–222

logging

adding to database interactions, 220

capturing return values, 228

for debugging, 219

load and save methods for managing, 221–222

passing arguments and, 227

silence method for turning off, 231

using code blocks for, 222–223

using explicit log messages, 220–221

long running tests, 110

lstrip method, for strings, 47

M

magic methods. see also method_missing, building APIs with

example in ActiveRecord, 291–292

example in OpenStruct class, 290–291

overview of, 288

map method, for collections, 35

Matsumoto, Yukihiro, 382

Matz’s Ruby Interpreter. see MRI (Matz’s Ruby Interpreter)

metaclasses. see singleton classes

metadata, gems and, 370, 373

metaprogramming

hooks. see hooks

monkey patching. see monkey patching

need for testing in, 315–316

overview of, 249

self modifying classes. see classes, self modifying

superclasses as basis for class modifying code. see classes, that modify subclasses

when to use, 331–332

method_added, 256

method_missing

types of hooks, 256

used in conjunction with internal DSL, 344

value of, 332

method_missing, building APIs with

avoiding trouble when using, 289–290

building form letters one word at a time, 286–288

examples of use of, 290–292

overview of, 285

review of, 292

user focus in creating easy-to-use APIs, 289

method_missing, delegation with

avoiding trouble when using, 279–281

example of use by ActiveRecord, 282–283

overview of, 273

problems with traditional style of delegation, 275

process of applying delegation, 277–278

pros/cons of delegation, 274–275

review of, 283

selective approach to delegation, 278–279

SimpleDelegator class, 281–282

method_missing, error handling with, 263–264

avoiding trouble when using, 270–271

handling document errors, 266–267

overriding, 265

review of, 271

what occurs when Ruby fails to find a method, 264–265

whiny nil facility in Rails as example of use of, 268–269

methods

array method-passing feature, 30–32

calling on object instances, 74–75

class methods that build instance methods, 321–323

classes as container for, 74

class/instance approximation in defining, 157

creating code blocks by tacking on to end of method calls, 207–208

define_method for creating, 324

defining module-level, 189

defining operators vs. using methods, 135

determining when methods are defined, 307

dynamic typing and, 85

for equality, 142–143

fundamental nature of method calls in Ruby, 81–82

handling missing. see method_missing

hash method-passing feature, 33

“if the method is there, it is the right object,” 94

inheriting default set from Object class, 77–78

looking for in superclasses, 75–76

mixing instance methods with class methods, 254–255

modifying classes and, 295

modifying subclasses from superclass methods, 324–326

modules as container for, 182, 184–185

naming conventions, 8

operator-to-method translation, 130

parentheses in calling/defining, 9–10

public, private, and protected, 79–81

public methods for arrays and hashes, 36

redefining on broken classes, 295–296

reflection-oriented, 79

renaming using alias_method, 297–299

singleton methods overriding class-defined methods, 159–160

that take code blocks, 223–224

methods, writing

ActiveRecord::Base class example, 127–128

composed method way of building classes, 122–123

compressing specifications, 117–121

overview of, 117

qualities of good methods, 121–122

review of, 128

single-exit approach, 123–126

troubles arising from decomposing methods, 126–127

MiniSpec, 110

MiniTest, 110

mixin modules

as alternative to superclasses, 193–195

avoiding trouble when using, 198–202

constants stored in, 204–205

DataMapper example of use of, 202–203

for extending modules, 197–198

inheritance relationships and, 201–202

overview of, 193

Rails helper methods using, 203–204

review of, 205

as solution for sharing code between unrelated classes, 195–197

mocha

singleton methods and, 165

utilities for Test::Unit, 109

mocks

RSpec, 107–108

singleton methods and, 165

models, object oriented programming as support system for, 157

modifier forms, of control structures, 19–20

modifiers, strings, 48

module variables, 178–179

module_eval, for creating methods, 329

modules

accessing classes in, 182–183

accessing constants in, 183

adding module variables to, 178–179

avoiding name collisions, 377–378

avoiding trouble when using, 189–190

benefits of dynamic typing, 85

building incrementally, 185

class hierarchy and, 201

as containers, 181–182

economical use of, 190–191

extending, 197–198

grouping related classes in, 182

grouping utility methods in, 184–185

hook for informing when a module gets included in a class, 253–255

including in classes, 195–196

mixing into class. see mixin modules

nesting, 183–184

review of, 191

treating as objects, 186–189

modulo (%) operator, 131, 152

monkey patching. see also open classes

do anything to any class, anytime, 297–299

examples of use of, 299–302

how it works, 307–308

modifying existing classes, 296–297

renaming methods using alias_method, 297–299

MRI (Matz’s Ruby Interpreter)

overview of, 382–385

support and documentation, 389

YARV as next generation implementation of, 385

multiline strings, 46, 61–62

multiplication (*) operator, 131

mutability, of strings, 51–52

N

names

accessing classes in modules by, 182–183

alias_method for renaming methods, 297–299

avoiding collisions, 377–378

example in set.rb class, 14

execute around and, 228–229

gems and, 371

method, 122

objects and name collisions, 82–83

Ruby conventions, 8–9

variable, 8

namespaces, creating name-space modules, 189

NaN (Not a Number), 296

nesting modules, 183–184

nil

boolean logic and, 23–25

initializing variables and, 26

as an object, 77, 84

whiny nil facility in Rails, 268–269

Not a Number (NaN), 296

not operator, 132

numeric classes

accepting Float as equals, 154–156

not supporting singleton methods, 159

O

object oriented programming

Ruby as OO programming language, 73

as support system, for models, 157

object relational mappers

ActiveRecord. see ActiveRecord

DataMapper. see DataMapper

objects

avoiding trouble when using, 82–84

BasicObject, 280–281

classes, instances, and methods, 74–76

consistency of Ruby object system, 76–77

dynamic typing. see dynamic typing

equality. see equality

fundamental nature of method calls in Ruby, 81–82

“if the method is there, it is the right object,” 94

initializing using execute around, 225, 229–230

methods, 77–79

modules as, 186–189

name collisions and, 82–83

Object class, 77

overview of, 73–74

public, private, and protected methods, 79–81

referencing with variables, 77

review of, 84

singleton methods, 158–159

ObjectSpace class, 217–218

open classes. see also monkey patching

avoiding trouble when using, 303

creating self-modifying classes, 305

defining classes, 294

examples of use of, 299–302

fixing broken classes, 295–296

improving existing classes, 296–297

modifying classes, 295

overview of, 293–294

renaming methods using alias_method, 297–299

review of, 303–304

value of, 332

OpenStruct class, 290–291

operators

cases/situations calling for, 135–137

commutative, 137

defining, 129–131

overview of, 129

review of, 139

string formatting, 137–138

types in Ruby, 131–133

using across classes, 134–135

or (|) operator, 131

order, of arrays and hashes, 38

overloading operators, 129

overriding methods

errors and, 83

method_missing, 265

methods in superclass unable to override methods in subclasses, 200

P

packaging programs, as gems. see gems

parentheses ( () )

readability and, 12

Ruby conventions for calling defining/methods, 9–10

parse_statement method, 357

parsers

based on regular expressions, 356–358

building for XML processing language, 353–356

examples of external DSLs, 364

HAML and, 361–362

Treetop for building, 358–360

Pathname class, 299–300

pattern matching, 150

period (.)

for matching any single character, 54

in module syntax, 185

using asterisk (*) in conjunction with, 58

polymorphism, 88

pop method, for arrays, 37

print method, Object class, 78

private methods, 79–81

Proc class, 239–241

Proc.new, 240–241

programming

metaprogramming. see metaprogramming

object oriented, 73, 157

trade offs in programming languages, 336

programming logic, adding to classes, 308–309, 314

programs, packaging as gems, 367

protected methods, 81

public methods

overview of, 79

returning all public methods of an object, 69

public_methods, Object class, 79

push method, for arrays, 37

puts method, Object class, 78

Q

question mark (?), using with regular expressions, 62–63

R

RACC, for building parsers, 359

Rails

example of const_missing hook, 270

example of on-the-fly class modification, 312–313

example of saved code blocks, 243

helper methods using mixins, 203–204

whiny nil facility, 268–269

Rake

as build tool, 335

example of const_missing hook, 269–270

example of saved code blocks, 243–244

examples of internal DSLs, 345–346

specifying executable scripts in gems, 374

rake command, 374

rake push command, 376

Rakefiles

automating creation of gems, 375–376

sow command generating, 377

ranges

of characters in regular expressions, 56

indexing strings and, 52

readability, of code, 12–13

reflection-oriented methods, 79

Regexp data type, 58

regular expressions

asterisk (*) symbol in, 57–58

case statement detecting match, 23

HAML and, 361

matching beginnings and endings of strings, 60–62

matching one character at a time, 54–55

mistakes to avoid, 63

as objects, 76

overview of, 53

parser based on, 356–358

pattern matching against strings, 150

resources for use of, 394

review of, 64

sets, ranges, and alternatives, 55–57

time.rb example, 62–63

repository, uploading gems to, 374–375

require method, Object class, 82

required type declarations, compensating for lost documentation due to, 92–93

Resolv class, 217

resources, for Ruby, 393–395

respond_to method, 146–147

return, in code blocks, 216

reverse method, for arrays, 36–37

REXML XML parsing library, 338–339

Ripper DSL, 352–353

RSpec

double-equals (==) operator, 138

examples, 104

independence of test, 111

internal DSLs and, 345–346

MiniSpec, 110

mocks, 107–109

overview of, 102–104

parameters, 105

saved code blocks and, 243

shoulda gem providing RSpec-like example, 108

singleton methods and, 165

specifying executable scripts in gems, 374

stubs, 106–107

as testing utility, 335

tidy and readable specs, 104–105

rstrip method, for strings, 47

Rubinius, 382, 388–389

Ruby implementations

avoiding trouble when using, 389

extending, 389

JRuby, 387–388

MRI, 382–385

overview of, 381

review of, 390

Rubinius, 388–389

versions and, 381–382

YARV, 385–387

Ruby versions

comparing Ruby versions, 381–382

managing transition between, 311–312

MRI supporting Ruby 1.8, 383

YARV supporting Ruby 1.9, 381–382

RubyForge, 375

RubyGems. see gems

ruby-mp3info, 368

RubySpec project, 109–110

run-time decisions, putting programming logic in classes and, 308

S

save methods, 221–222

scope (closure)

avoiding trouble when using, 241–242

code blocks drag scope along to wherever they are applied, 225–227

scope, of class methods, 165–166

scripts, specifying executable scripts in gems, 374

self

class methods and, 309

as default object in method calls, 75

knowing value of during class definition, 330–331

semicolon (;), for separating statements in Ruby code, 10–11

set

regular expression for matching any one of a bunch of characters, 55–56

using asterisk (*) in conjunction with, 58

Set class

dynamic typing and, 95–96

mapping boolean operators to union and intersection operations, 135

set_trace_func hook, 256–257

setup method, Test::Unit, 100

shift method, for arrays, 37

shoulda gem, utilities for Test::Unit, 108

silence method, for turning logging off, 231

SimpleDelegator class, 281–282

simplicity, as solution to code complexity, 92

single quotes (′), use with string literals, 44–45

single-exit approach, to writing methods, 123–126

singleton classes

adding convenience to class instance variables, 176–177

class methods, 162–165

visibility of, 160–161

singleton methods

alternative syntax for, 160

avoiding trouble when using, 165–167

class methods, 162–165

defining, 158, 163–164

extending modules and, 198

invisibility of singleton class, 160–161

overriding class-defined methods, 159–160

overview of, 157–158

review of, 167

software

resources for building software with Ruby, 394

trade offs in software engineering, 335

sort method

<=> operator and, 214

for arrays, 37

source code, for Ruby projects, 394

sow command, generating directory structure of gems, 377

spec command

running specifications with, 103–104

specifying executable scripts in gems, 374

specs. see also tests

MiniSpec, 110

mocks and, 107–108

overview of, 103

RubySpec project, 109–110

running with spec command, 103–104

stubs and, 105–107

tidy and readable, 104–105

when to write, 113

splat, for star jargon, 32

split method, working with strings, 48

square brackets. see [ ] (square brackets)

squish! method, adding to String class, 301–302

static typing

adding type-checking code to methods and, 91

bulkier code with, 89

dangers of dynamic typing and, 93

overview of, 85

StringIO class, comparing with File class, 94–95

strings. see also regular expressions

adding methods to String class, 300–302

APIs supported, 47–49

converting symbols to/from, 69

formatting operator (%) for, 137–138

indexing, 52

inflection rules based on gsub, 50–51

mutability of, 51–52

as objects, 76

optimizing String class for data processing, 67

options for writing, 44–46

overview of, 43

pattern matching regular expressions against strings, 150

review of, 52

String class, 43

symbols as, 65–66

types of thing collected in, 49–50

uses of, 66–67

when to use symbols vs. when to use strings, 70–71

strip method, 47

stubs

RSpec, 105–107

singleton methods and, 165

sub method, working with strings, 47–48

subclasses

calling private methods from, 80

class instance variables and, 175–176

difficulty of subclassing, 319–321

example of paragraph subclass, 317–319

examples of subclass-changing methods, 327

hook for informing when a class gains a subclass, 250–253, 257–259

methods in superclass unable to override methods in subclasses, 200

no limit to modifying from superclass method, 324–326

practical basis of, 95

subtraction (-) operator

as binary or unary operator, 132

overloading, 131

sudo, for running gems, 368

superclasses

in inheritance tree, 193

methods in superclass that can add methods to subclasses, 324

methods in superclass unable to override methods in subclasses, 200

mixins as alternative to, 193–195

modules and, 198

no limit to modifying subclasses from superclass method, 324–326

overview of, 75–76

swapcase method, working with strings, 47

switch statement, case statement compared with, 21

symbols

compared with strings, 65–66

confusing nature of, 69–70

converting strings to/from, 69

as hash keys, 68–69

immutability of, 68

not supporting singleton methods, 159

as objects, 76

overview of, 65

review of, 71

single instance of, 67–68

using strings as symbolic markers, 66–67

when to use symbols vs. when to use strings, 70–71

symmetry principal, double-equals (==) operator and, 146–147

T

tabs, Ruby indentation conventions and, 5–6

TAR files, 370

teardown method, Test::Unit, 100

ternary operator (?:), in expression-based decision making, 26

test directory, sow command generating, 377

test-first development, 113

tests

applying to gems, 380

assertions in Test::Unit, 101

automated testing for resolving bugs, 94

limitations of Test::Unit, 101–102

MiniTest, 110

mocha utilities for Test::Unit, 109

mocks and, 107–108

overview of, 97

qualities of good tests, 110–113

review of, 113

RSpec testing framework, 102–104

RubySpec project, 109–110

shoulda gem utilities for Test::Unit, 108

stubs and, 105–107

tidy and readable specs, 104–105

when to write, 113

Test::Unit

at_exit hook used in, 259–260

assertions in, 101

limitations of, 101–102

mocha utilities for, 109

overview of, 98–100

shoulda gem utilities for, 108

text processing, strings and, 43

TextCompressor class, 119

time zones, regular expression for offsetting, 62–63

time.rb, regular expressions and, 62–63

times method, iterators, 211–212

to_s method

of Object class, 77–78

turning symbols into strings, 69

to_sym method, turning strings into symbols, 69

transitive property, of double-equals (==) operator, 147–149

Treetop

for building parsers, 358–360

examples of external DSLs, 364

triple equals operator (===), for case statements, 23, 149–150

true, as an object, 76

two space rule, Ruby indentation convention, 5–6

type declaration

documentation and, 92

dynamic typing. see dynamic typing

static typing. see static typing

type-checking code, disadvantages of adding, 91

U

unary operators, 131–132, 134

union operations, mapping boolean operators to, 135

unique_index_of method, 120

unit tests. see also Test::Unit

minimum tests, 112–113

speed as factor in, 110

Unix, 370

unless statements

example of use of, 18–19

modifier forms of, 20

until statements

comments, 6

example of use of, 19

modifier forms of, 20

upcase method, working with strings, 47

URIs

using class variables with, 177–178

using modules with, 191

user interfaces, 285. see also APIs

V

values

case statement returning, 22

code blocks returning, 208–209

variables

adding module variables to modules, 178–179

attaching instance variable to class objects (class instance variables), 174–175

class variables. see class variables

documenting declaration of, 92

initializing, 26

modules and, 186

naming, 8

open classes and, 294

referencing objects with, 77

VCIS (Version Conflict Induced Insanity), 370

versions

Ruby implementations and, 381–382

versioning support in gems, 368–369

vertical bar (|), in syntax of alternatives in regular expressions, 56–57

visibility, of methods, 79–81

W

while statements

code capturing values of, 25

example of use of, 19

modifier forms of, 20

whiny nil facility, Rails, 268–269

white space, managing in strings, 47

with_logging methods

capturing return values, 228

managing logging with, 222

passing arguments and, 227

X

XML

accessing/manipulating data in, 336–337

building parser for, 353–356

creating reader for, 251–252

processing in Ruby with REXML, 337–339

XmlRipper class for writing XML processing scripts, 340–341

XmlRipper class

building parser for XML processing language, 354

transition from API to DSL and, 341–344

for writing XML processing scripts, 340

XPath, 338–339, 344

XSLT, 337

XUnit testing frameworks, 98

Y

YAML

compared with XML, 250

example of use of modules in, 191

YARV

overview of, 385–387

support and documentation, 389

yield, firing code blocks, 233

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.74.160