Home > Store > Programming > General Programming/Other Languages

Writing Efficient Ruby Code (Digital Short Cut)

Register your product to gain access to bonus material or receive a coupon.

Writing Efficient Ruby Code (Digital Short Cut)

eBook (Watermarked)

  • Your Price: $12.99
  • About Watermarked eBooks
  • This PDF will be accessible from your Account page after purchase and requires the free Adobe® Reader® software to read it.

    The eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.

    Watermarked eBook FAQ

Description

  • Copyright 2008
  • Dimensions: 7x9-1/4
  • Pages: 50
  • Edition: 1st
  • eBook (Watermarked)
  • ISBN-10: 0-321-54003-4
  • ISBN-13: 978-0-321-54003-4

This short cut focuses on a number of coding patterns that are useful when trying to get maximum speed out of performance-critical sections of Ruby code. Anti-patterns, that is, coding idioms, which should be avoided in performance-sensitive code sections are discussed, including details on how to transform such code to make it more efficient. Most patterns were extracted from Stefan Kaes' work on improving performance of the Rails core and his regular Rails performance consulting work. These patterns are largely non-algorithmic, detailing local code transformations to achieve identical results with slightly different and faster code, as even local code changes can sometimes result in orders of magnitude improvements. Some patterns are useful independent of Rails' implementation language, but some of them are specific to Ruby, or more specifically, the current implementation of Ruby. Converts from other languages, especially from statically typed languages such as Java or C++ may find this material useful, as the performance characteristics of certain operations, like performing a function call or accessing object fields/attributes, are quite different from what you expect.

What This Short Cut Covers

Introduction

Ruby's Interpreter Is Slow

Runtime Complexity of Ruby Language Constructs

Patterns

Instance Variables versus Accessors

Local Variables Are Cheap

Assignments in Expressions

Interpolated Strings

In-Place Updates

Sets versus Arrays

For Loops versus each

Make Decisions at Load Time

Self Modifying Code

Test Most Frequent Case First

Optimize Access to Global Constants

Caching Data in Instance Variables

Caching Data in Class Variables

Coding Variable Caching Efficiently

Initializing Variables with nil

Using .nil?

nil? or empty? versus blank?

Using return

Using returning

Using any?

Block Local Variables

Date Formatting

Temporary Datastructure Constants

File System Access

ObjectSpace.each_object

Unnecessary Block Parameters

Symbol.to_proc

Chained Calls of map

Requiring Files Dynamically

Including Modules versus Opening Classes

About the Author

Sample Content

Sample Pages

Download pages 17 through 19

Download pages 30 and 31

Table of Contents

What This Short Cut Covers

Introduction

Ruby's Interpreter Is Slow

Runtime Complexity of Ruby Language Constructs

Patterns

Instance Variables versus Accessors

Local Variables Are Cheap

Assignments in Expressions

Interpolated Strings

In-Place Updates

Sets versus Arrays

For Loops versus each

Make Decisions at Load Time

Self Modifying Code

Test Most Frequent Case First

Optimize Access to Global Constants

Caching Data in Instance Variables

Caching Data in Class Variables

Coding Variable Caching Efficiently

Initializing Variables with nil

Using .nil?

nil? or empty? versus blank?

Using return

Using returning

Using any?

Block Local Variables

Date Formatting

Temporary Datastructure Constants

File System Access

ObjectSpace.each_object

Unnecessary Block Parameters

Symbol.to_proc

Chained Calls of map

Requiring Files Dynamically

Including Modules versus Opening Classes

About the Author

Introduction

Download the introduction

Updates

Submit Errata

More Information

Unlimited one-month access with your purchase
Free Safari Membership