Polished Ruby Programming

Book description

Elevate your Ruby skills to an advanced level by deepening your understanding of the design principles, best practices, and trade-offs involved in implementation approaches to future-proof your Ruby applications

Key Features

  • Learn Ruby web application design principles and strategies for databases, security, and testing from a Ruby committer
  • Understand the design principles behind polished Ruby code and trade-offs between implementation approaches
  • Use metaprogramming and DSLs to reduce the amount of code needed without decreasing maintainability

Book Description

Anyone striving to become an expert Ruby programmer needs to be able to write maintainable applications.

Polished Ruby Programming will help you get better at designing scalable and robust Ruby programs, so that no matter how big the codebase grows, maintaining it will be a breeze.

This book takes you on a journey through implementation approaches for many common programming situations, the trade-offs inherent in each approach, and why you may choose to use different approaches in different situations.

You'll start by refreshing Ruby fundamentals, such as correctly using core classes, class and method design, variable usage, error handling, and code formatting. Then you'll move on to higher-level programming principles, such as library design, use of metaprogramming and domain-specific languages, and refactoring. Finally, you'll learn principles specific to web application development, such as how to choose a database and web framework, and how to use advanced security features.

By the end of this Ruby programming book, you'll be a well rounded web developer with a deep understanding of Ruby.

While most code examples and principles discussed in the book apply to all Ruby versions, some examples and principles are specific to Ruby 3.0, the latest release at the time of publication.

What you will learn

  • Use Ruby's core classes and design custom classes effectively
  • Explore the principles behind variable usage and method argument choice
  • Implement advanced error handling approaches such as exponential backoff
  • Design extensible libraries and plugin systems in Ruby
  • Use metaprogramming and DSLs to avoid code redundancy
  • Implement different approaches to testing and understand their trade-offs
  • Discover design patterns, refactoring, and optimization with Ruby
  • Explore database design principles and advanced web app security

Who this book is for

This book is for Ruby programmers who are comfortable in coding with Ruby but want to advance their skills by mastering the deeper principles and best practices behind writing maintainable, scalable, optimized, and well-structured Ruby code. This book won't teach you the basics of Ruby – you'll need intermediate knowledge and practical experience before you can dive in.

Table of contents

  1. Polished Ruby Programming
  2. Contributors
  3. About the reviewers
  4. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Reviews
  5. Section 1: Fundamental Ruby Programming Principles
  6. Chapter 1: Getting the Most out of Core Classes
    1. Technical requirements
    2. Learning when to use core classes
    3. Best uses for true, false, and nil objects
    4. Different numeric types for different needs
    5. Understanding how symbols differ from strings
    6. Learning how best to use arrays, hashes, and sets
      1. Implementing an in-memory database
    7. Working with Struct – one of the underappreciated core classes
    8. Summary
    9. Questions
    10. Further reading
  7. Chapter 2: Designing Useful Custom Classes
    1. Technical requirements
    2. Learning when to create a custom class
    3. Handling trade-offs in SOLID design
      1. The single-responsibility principle
      2. The open-closed principle
      3. The Liskov substitution principle
      4. The interface segregation principle
      5. The dependency inversion principle
    4. Deciding on larger classes or more classes
    5. Learning when to use custom data structures
    6. Summary
    7. Questions
  8. Chapter 3: Proper Variable Usage
    1. Technical requirements
    2. Using Ruby's favorite variable type – the local variable
      1. Increasing performance by adding local variables
      2. Avoiding unsafe optimizations
      3. Handling scope gate issues
      4. Naming considerations with local variables
    3. Learning how best to use instance variables
      1. Increasing performance with instance variables
      2. Handling scope issues with instance variables
      3. Naming considerations for instance variables
    4. Understanding how constants are just a type of variable
      1. Handling scope issues with constants
      2. Visibility differences between constants and class instance variables
      3. Naming considerations with constants
    5. Replacing class variables
      1. Replacing class variables with constants
      2. Replacing class variables with class instance variables using the superclass lookup approach
      3. Replacing class variables with class instance variables using the copy to subclass approach
    6. Avoiding global variables, most of the time
    7. Summary
    8. Questions
    9. Further reading
  9. Chapter 4: Methods and Their Arguments
    1. Technical requirements
    2. Understanding that there are no class methods, only instance methods
    3. Naming methods
      1. Special method names
    4. Using the many types of method arguments
      1. Positional arguments
      2. Optional positional arguments
      3. Rest arguments
      4. Keyword arguments
      5. Block arguments
    5. Learning about the importance of method visibility
      1. Fixing visibility mistakes
    6. Handling delegation
      1. Delegating to other objects
    7. Summary
    8. Questions
  10. Chapter 5: Handling Errors
    1. Technical requirements
    2. Handling errors with return values
    3. Handling errors with exceptions
      1. Considering performance when using exceptions
    4. Retrying transient errors
      1. Understanding more advanced retrying
      2. Breaking circuits
    5. Designing exception class hierarchies
      1. Using core exception classes
    6. Summary
    7. Questions
  11. Chapter 6: Formatting Code for Easy Reading
    1. Technical requirements
    2. Recognizing different perspectives of code formatting
    3. Learning how syntactic consistency affects maintainability
      1. Enforcing consistency with RuboCop
    4. Understanding the consequences of using arbitrary limits
    5. Checking basic code formatting with Ruby
    6. Realizing the actual importance of code formatting
    7. Summary
    8. Questions
  12. Section 2: Ruby Library Programming Principles
  13. Chapter 7: Designing Your Library
    1. Technical requirements
    2. Focusing on the user experience
      1. Library naming
      2. Library first impressions
      3. The simplest possible interface
    3. Determining the appropriate size for your library
    4. Handling complexity trade-offs during method design
      1. Fewer but more complex methods
    5. Summary
    6. Questions
  14. Chapter 8: Designing for Extensibility
    1. Technical requirements
    2. Using Ruby's extensibility features
    3. Designing plugin systems
      1. Designing a basic plugin system
      2. Handling changes to classes
      3. Plugin modifications to classes
      4. Supporting plugin dependencies
      5. Making plugin loading easier
      6. Handling subclasses in plugin systems
      7. Configuring plugins
    4. Understanding globally frozen, locally mutable design
    5. Summary
    6. Questions
  15. Chapter 9: Metaprogramming and When to Use It
    1. Technical requirements
    2. Learning the pros and cons of abstraction
    3. Eliminating redundancy
    4. Understanding different ways of metaprogramming methods
    5. Using method_missing judiciously
    6. Summary
    7. Questions
  16. Chapter 10: Designing Useful Domain-Specific Languages
    1. Technical requirements
    2. Designing your DSL
      1. Configuration DSLs
      2. DSLs for making specific changes
      3. DSLs for reducing the verbosity of code
      4. Libraries implemented as DSLs
    3. Implementing your DSL
    4. Learning when to use a DSL
    5. Summary
    6. Questions
  17. Chapter 11: Testing to Ensure Your Code Works
    1. Technical requirements
    2. Understanding why testing is so critical in Ruby
    3. Learning different approaches to testing
    4. Considering test complexity
    5. Understanding the many levels of testing
    6. Realizing that 100% coverage means nothing
    7. Summary
    8. Questions
  18. Chapter 12: Handling Change
    1. Technical requirements
    2. Considering reasons to refactor
    3. Learning about the refactoring process
    4. Implementing the most common Ruby refactoring techniques
      1. Extracting a method
      2. Extracting a class
    5. Refactoring to add features
    6. Removing features properly
      1. Removing methods
      2. Removing constants
    7. Summary
    8. Questions
  19. Chapter 13: Using Common Design Patterns
    1. Technical requirements
    2. Learning about the many design patterns that are built into Ruby
      1. The object pool design pattern
      2. The prototype design pattern
      3. The private class data design pattern
      4. The proxy design pattern
    3. Handling cases where there can be only one
    4. Dealing with nothing
    5. Visiting objects
    6. Adapting and strategizing
    7. Summary
    8. Questions
  20. Chapter 14: Optimizing Your Library
    1. Technical requirements
    2. Understanding that you probably don't need to optimize code
    3. Profiling first, optimizing second
    4. Understanding that no code is faster than no code
    5. Handling code where everything is slow
    6. Summary
    7. Questions
  21. Section 3: Ruby Web Programming Principles
  22. Chapter 15: The Database Is Key
    1. Technical requirements
    2. Learning why database design is so important
    3. Deciding on a database to use
    4. Understanding the most important database design principles
      1. Considerations when denormalizing your database design
      2. Other database design principles
    5. Treating the database as not just dumb storage
    6. Choosing the model layer
    7. Handling database and model errors
    8. Summary
    9. Further reading
    10. Questions
  23. Chapter 16: Web Application Design Principles
    1. Technical requirements
    2. Choosing between client-side and server-side design
    3. Deciding on a web framework
      1. Ruby on Rails
      2. Sinatra
      3. Grape
      4. Roda
    4. Designing URL paths
    5. Structuring with monoliths, microservices, and island chains
    6. Summary
    7. Questions
  24. Chapter 17: Robust Web Application Security
    1. Technical requirements
    2. Understanding that most security issues in Ruby web applications are high level
    3. Never trust input
    4. Performing access control at the highest level possible
    5. Avoiding injection
      1. Script injection
      2. SQL injection
      3. Code injection
    6. Approaching high-security environments
      1. Limiting database access
      2. Internal firewalling
      3. Randomizing memory layouts
      4. Limiting filesystem access
      5. Limiting system call access
    7. Summary
    8. Questions
  25. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
    15. Chapter 15
    16. Chapter 16
    17. Chapter 17
    18. Why subscribe?
  26. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think

Product information

  • Title: Polished Ruby Programming
  • Author(s): Jeremy Evans
  • Release date: July 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781801072724