Ruby for Rails
Ruby techniques for Rails developers
David A. Black
Foreword by David Heinemeier Hansson
  • May 2006
  • ISBN 9781932394696
  • 532 pages

Closes the gap between Ruby as a language and Rails as a framework. The breadth of knowledge is astounding!

Benjamin S. Gorlick, Software Engineer and Developer


The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition!

Ruby for Rails helps Rails developers achieve Ruby* mastery. Each chapter deepens your Ruby knowledge and shows you how it connects to Rails. You'll gain confidence working with objects and classes and learn how to leverage Ruby's elegant, expressive syntax for Rails application power. And you'll become a better Rails developer through a deep understanding of the design of Rails itself and how to take advantage of it.

*Learn Ruby 1.9 from David A. Black in The Well-Grounded Rubyist.

About the Technology

The word is out: with Ruby on Rails you can build powerful Web applications easily and quickly! And just like the Rails framework itself, Rails applications are Ruby programs. That means you can't tap into the full power of Rails unless you master the Ruby language.

Table of Contents detailed table of contents




about this book

about the cover illustration

Part 1 The Ruby/Rails landscape

1. How Ruby works

1.1. The mechanics of writing a Ruby program

1.1.1. Getting the preliminaries in place

1.1.2. A Ruby literacy bootstrap guide

1.1.3. A brief introduction to method calls and Ruby objects

1.1.4. Writing and saving a sample program

1.1.5. Feeding the program to Ruby

1.1.6. Keyboard and file input

1.1.7. One program, multiple files

1.2. Techniques of interpreter invocation

1.2.1. Command-line switches

1.2.2. A closer look at interactive Ruby interpretation with irb

1.3. Ruby extensions and programming libraries

1.3.1. Using standard extensions and libraries

1.3.2. Using C extensions

1.3.3. Writing extensions and libraries

1.4. Anatomy of the Ruby programming environment

1.4.1. The layout of the Ruby source code

1.4.2. Navigating the Ruby installation

1.4.3. Important standard Ruby tools and applications

1.5. Summary

2. How Rails works

2.1. Inside the Rails framework

2.1.1. A framework user’s-eye view of application development

2.1.2. Introducing the MVC framework concept

2.1.3. Meet MVC in the (virtual) flesh

2.2. Analyzing Rails' implementation of MVC

2.3. A Rails application walk-through

2.3.1. Introducing R4RMusic, the music-store application

2.3.2. Modeling the first iteration of the music-store domain

2.3.3. Identifying and programming the actions

2.3.4. Designing the views

2.3.5. Connecting to the application

2.4. Tracing the lifecycle of a Rails run

2.4.1. Stage 1: server to dispatcher

2.4.2. Stage 2: dispatcher, to controller

2.4.3. Stage 3: performance of a controller action

2.4.4. Stage 4: the fulfillment of the view

2.5. Summary

3. Ruby-informed Rails development

3.1. A first crack at knowing what your code does

3.1.1. Seeing Rails as a domain-specific language

3.1.2. Writing program code with a configuration flavor

3.1.3. YAML and configuration that’s actually programming

3.2. Starting to use Ruby to do more in your code

3.2.1. Adding functionality to a controller

3.2.2. Deploying the Rails helper files

3.2.3. Adding functionality to models

3.3.1. Converting legacy data to ActiveRecord

3.3.2. The irb-based Rails application console

3.4. Summary

Part 2 Ruby building blocks

4. Objects and variables

4.1. From "things" to objects

4.1.1. Introducing object-oriented programming

4.1.2. I, object!

4.1.3. Modeling objects more closely: the behavior of a ticket

4.2. The innate behaviors of an object

4.2.1. Identifying objects uniquely with the object_id method

4.2.2. Querying an object’s abilities with the respond_to? method

4.2.3. Sending messages to objects with the send method

4.3. Required, optional, and default-valued arguments

4.3.1. Required and optional arguments

4.3.2. Default values for arguments

4.3.3. Order of arguments

4.4. Local variables and variable assignment

4.4.1. Variable assignment in depth

4.4.2. Local variables and the things that look like them

4.5. Summary

5. Organizing objects with classes

5.1. Classes and instances

5.1.1. A first class

5.1.2. Instance variables and object state

5.2. Setter methods

5.2.1. The equal sign (=) in method names

5.2.2. ActiveRecord properties and other =-method applications

5.3. Attributes and the attr_* method family

5.3.1. Automating the creation of attribute handlers

5.3.2. Two (getter/setter) for one

5.3.3. Summary of attr_* methods

5.4. Class methods and the Class class

5.4.1. Classes are objects too!

5.4.2. When, and why, to write a class method

5.4.3. Class methods vs. instance methods, clarified

5.4.4. The Class class and

5.5. Constants up close

5.5.1. Basic usage of constants

5.5.2. Reassigning vs. modifying constants

5.6. Inheritance

5.6.1. Inheritance and Rails engineering

5.6.2. Nature vs. nurture in Ruby objects

5.7. Summary

6. Modules and program organization

6.1. Basics of module creation and use

6.1.1. A module encapsulating "stack-like-ness"

6.1.2. Mixing a module into a class

6.1.3. Leveraging the module further

6.2. Modules, classes, and method lookup

6.2.1. Illustrating the basics of method lookup

6.2.2. Defining the same method more than once

6.2.3. Going up the method search path with super

6.3. Class/module design and naming

6.3.1. Mix-ins and/or inheritance

6.3.2. Modular organization in Rails source and boilerplate code

6.4. Summary

7. The default object (self) and scope

7.1. Understanding self, the current/default object

7.1.1. Who gets to be self, and where

7.1.2. Self as default receiver of messages

7.1.3. Instance variables and self

7.2. Determining scope

7.2.1. Global scope and global variables

7.2.2. Local scope

7.2.3. Scope and resolution of constants

7.3. Deploying method access rules

7.3.1. Private methods

7.3.2. Private methods as ActionController access protection

7.3.3. Protected methods

7.4. Writing and using top-level methods

7.4.1. Defining a top-level method

7.4.2. Predefined (built-in) top-level methods

7.5. Summary

8. Control flow techniques

8.1. Conditional code execution

8.1.1. The if keyword and friends

8.1.2. Conditional modifiers

8.1.3. Case statements

8.2. Repeating actions with loops

8.2.1. Unconditional looping with the loop method

8.2.2. Conditional looping with the while and until keywords

8.2.3. Looping based on a list of values

8.3. Code blocks, iterators, and the yield keyword

8.3.1. The basics of yielding to a block

8.3.2. Performing multiple iterations

8.3.3. Using different code blocks

8.3.4. More about for

8.4. Error handling and exceptions

8.4.1. Raising and rescuing exceptions

8.4.2. Raising exceptions explicitly

8.4.3. Creating your own exception classes

8.5. Summary

Part 3 Built-in classes and modules

9. Built-in essentials

9.1. Ruby’s literal constructors

9.2. Recurrent syntactic sugar

9.2.1. Special treatment of +=

9.3. Methods that change their receivers (or don’t)

9.3.1. Receiver-changing basics

9.3.2. bang (!) methods

9.3.3. Specialized and extended receiver-changing in ActiveRecord objects

9.4. Built-in and custom to_* (conversion) methods

9.4.1. Writing your own to_* methods

9.5. Iterators reiterated

9.6. Boolean states, Boolean objects, and nil

9.6.1. True and false as states

9.6.2. true and false as objects

9.6.3. The special object nil

9.7. Comparing two objects

9.7.1. Equality tests

9.7.2. Comparisons and the Comparable module

9.8. Listing an object’s methods

9.8.1. Generating filtered and selective method lists

9.9. Summary

10. Scalar objects

10.1. Working with strings

10.1.1. String basics

10.1.2. String operations

10.1.3. Comparing strings

10.2. Symbols and their uses

10.2.1. Key differences between symbols and strings

10.2.2. Rails-style method arguments, revisited

10.3. Numerical objects

10.3.1. Numerical classes

10.3.2. Performing arithmetic operations

10.4. Times and dates

10.5. Summary

11. Collections, containers, and enumerability

11.1. Arrays and hashes compared

11.2. Using arrays

11.2.1. Creating a new array

11.2.2. Inserting, retrieving, and removing array elements

11.2.3. Combining arrays with other arrays

11.2.4. Array transformations

11.2.5. Array iteration, filtering, and querying

11.2.6. Ruby lessons from ActiveRecord collections

11.3. Hashes

11.3.1. Creating a new hash

11.3.2. Inserting, retrieving, and removing hash pairs

11.3.3. Combining hashes with other hashes

11.3.4. Hash transformations

11.3.5. Hash iteration, filtering, and querying

11.3.6. Hashes in Ruby and Rails method calls

11.4. Collections central: the Enumerable module

11.4.1. Gaining enumerability through each

11.4.2. Strings as Enumerables

11.5. Sorting collections

11.5.1. Sorting and the Comparable module

11.5.2. Defining sort order in a block

11.6. Summary

12. Regular expressionsand regexp-basedstring operations

12.1. What are regular expressions?

12.1.1. A word to the regex-wise

12.1.2. A further word to everyone

12.2. Writing regular expressions

12.2.1. The regular expression literal constructor

12.2.2. Building a pattern

12.3. More on matching and MatchData

12.3.1. Capturing submatches with parentheses

12.3.2. Match success and failure

12.4. Further regular expression techniques

12.4.1. Quantifiers and greediness

12.4.2. Anchors and lookahead assertions

12.4.3. Modifiers

12.4.4. Converting strings and regular expressions to each other

12.5. Common methods that use regular expressions

12.5.1. String#scan

12.5.2. String#split

12.5.3. sub/sub! and gsub/gsub!

12.5.4. grep

12.6. Summary

13. Ruby dynamics

13.1. The position and role of singleton classes

13.1.1. Where the singleton methods live

13.1.2. Examining and modifying a singleton class directly

13.1.3. Singleton classes on the method lookup path

13.1.4. Class methods in (even more) depth

13.2. The eval family of methods

13.2.1. eval

13.2.2. instance_eval

13.2.3. The most useful eval: class_eval (a.k.a. module_eval)

13.3. Callable objects

13.3.1. Proc objects

13.3.2. Creating anonymous functions with the lambda keyword

13.3.3. Code blocks, revisited

13.3.4. Methods as objects

13.4. Callbacks and hooks

13.4.1. Intercepting unrecognized messages with method_missing

13.4.2. Trapping include operations with Module#included

13.4.3. Intercepting inheritance with Class#inherited

13.4.4. Module#const_missing

13.5. Overriding and adding to core functionality

13.5.1. A cautionary tale

13.6. Summary

Part 4 Rails through Ruby, Ruby throug Rails

14. (Re)modeling the R4RMusic application universe

14.1. Tracking the capabilities of an ActiveRecord model instance

14.1.1. An overview of model instance capabilities

14.1.2. Inherited and automatic ActiveRecord model behaviors

14.1.3. Semi-automatic behaviors via associations

14.2. Advancing the domain model

14.2.1. Abstracting and adding models (publisher and edition)

14.2.2. The instruments model and many-to-many relations

14.2.3. Modeling for use: customer and order

14.3. Summary

15. Programmatically enhancing ActiveRecord models

15.1. Soft vs. hard model enhancement

15.1.1. An example of model-enhancement contrast

15.2. Soft programmatic extension of models

15.2.1. Honing the Work model through soft enhancements

15.2.2. Modeling the customer’s business

15.2.3. Fleshing out the Composer

15.2.4. Ruby vs. SQL in the development of soft enhancements

15.3. Hard programmatic enhancement of model functionality

15.3.1. Prettification of string properties

15.3.2. Calculating a work’s period

15.3.3. The remaining business of the Customer

15.4. Extending model functionality with class methods

15.4.1. Soft and hard class methods

15.5. Summary

16. Enhancing the controllers and views

16.1. Defining helper methods for view templates

16.1.1. Organizing and accessing custom helper methods

16.1.2. The custom helper methods for R4RMusic

16.2. Coding and deploying partial view templates

16.2.1. Anatomy of a master template

16.2.2. Using partials in the welcome view template

16.3. Updating the main controller

16.3.1. The new face of the welcome action

16.4. Incorporating customer signup and login

16.4.1. The login and signup partial templates

16.4.2. Logging in and saving the session state

16.4.3. Gate-keeping the actions with before_filter

16.4.4. Implementing a signing-up facility

16.4.5. Scripting customer logout

16.5. Processing customer orders

16.5.1. The view_cart action and template

16.5.2. Viewing and buying an edition

16.5.3. Defining the add_to_cart action

16.5.4. Completing the order(s)

16.6. Personalizing the page via dynamic code

16.6.1. From rankings to favorites

16.6.2. The favorites feature in action

16.7. Summary

17. Techniques for exploring the Rails source code

17.1. Exploratory technique 1: panning for info

17.1.1. Sample info panning: belongs_to

17.2. Exploratory technique 2: shadowing Ruby

17.2.1. Choosing a starting point

17.2.2. Choose among forks in the road intelligently

17.2.3. On the trail of belongs_to

17.2.4. A transliteration of belongs_to

17.3. Exploratory technique 3: consulting the documentation

17.3.1. A roadmap of the online Rails API documentation

17.4. Summary

Appendix A: Ruby and Rails installation and resources

A.1. Online resources for Ruby and Rails

A.2. Common-case instructions for installing Ruby and Rails

A.2.1. One-Click Ruby and Instant Rails for Windows

A.2.2. Installing from the source code on *nix systems

A.2.3. Installing Ruby with a package manager

A.2.4. Installing the RubyGems package manager

A.2.5. Installing Rails with RubyGems


What's inside

  • Classes, modules, and objects
  • Collection handling and filtering
  • String and regular expression manipulation
  • Exploration of the Rails source code
  • Ruby dynamics
  • Many more programming concepts and techniques!

About the reader

Newcomers to Ruby will find a Rails-oriented Ruby introduction that's easy to read and that includes dynamic programming techniques, an exploration of Ruby objects, classes, and data structures, and many neat examples of Ruby and Rails code in action.

Ruby for Rails: the Ruby guide for Rails developers!

About the author

David A. Black has been programming in Ruby since 2000. He is the owner and director of the consultancy Ruby Power and Light, LLC, and a director of Ruby Central, Inc. David is a Ruby library contributor, and an expert practitioner and trainer in the Ruby language and the Ruby on Rails development framework.

David A. Black was a keynote presenter at the YVR06 Canada on Rails Conference, April 13-14, 2006 in Vancouver, BC, Canada.

placing your order...

Don't refresh or navigate away from the page.
customers also reading

This book 1-hop 2-hops 3-hops

FREE domestic shipping on three or more pBooks