0%

Become an accomplished Ruby programmer by understanding the design principles, best practices, and trade-offs involved in implementation approaches to keep your Ruby applications maintainable in the long term

Key Features

  • 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
  • Learn Ruby web application design principles and strategies for databases, security, and testing

Book Description

Most successful Ruby applications become difficult to maintain over time as the codebase grows in size. Polished Ruby Programming provides you with recommendations and advice for designing Ruby programs that are easy to maintain in the long term.

This book takes you 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 learning fundamental Ruby programming principles, such as correctly using core classes, class and method design, variable usage, error handling, and code formatting. Moving on, you'll learn 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 have gained the skills you need to design robust, high-performance, scalable, and maintainable Ruby applications.

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

If you already know how to program in Ruby and want to learn more about the principles and best practices behind writing maintainable, scalable, optimized, and well-structured Ruby code, then this Ruby book is for you. Intermediate to advanced-level working knowledge of the Ruby programming language is expected to get the most out of this book.

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
    7. Implementing an in-memory database
    8. Working with Struct – one of the underappreciated core classes
    9. Summary
    10. Questions
    11. 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
    4. The single-responsibility principle
    5. The open-closed principle
    6. The Liskov substitution principle
    7. The interface segregation principle
    8. The dependency inversion principle
    9. Deciding on larger classes or more classes
    10. Learning when to use custom data structures
    11. Summary
    12. Questions
  8. Chapter 3: Proper Variable Usage
    1. Technical requirements
    2. Using Ruby's favorite variable type – the local variable
    3. Increasing performance by adding local variables
    4. Avoiding unsafe optimizations
    5. Handling scope gate issues
    6. Naming considerations with local variables
    7. Learning how best to use instance variables
    8. Increasing performance with instance variables
    9. Handling scope issues with instance variables
    10. Naming considerations for instance variables
    11. Understanding how constants are just a type of variable
    12. Handling scope issues with constants
    13. Visibility differences between constants and class instance variables
    14. Naming considerations with constants
    15. Replacing class variables
    16. Replacing class variables with constants
    17. Replacing class variables with class instance variables using the superclass lookup approach
    18. Replacing class variables with class instance variables using the copy to subclass approach
    19. Avoiding global variables, most of the time
    20. Summary
    21. Questions
    22. 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
    4. Special method names
    5. Using the many types of method arguments
    6. Positional arguments
    7. Optional positional arguments
    8. Rest arguments
    9. Keyword arguments
    10. Block arguments
    11. Learning about the importance of method visibility
    12. Fixing visibility mistakes
    13. Handling delegation
    14. Delegating to other objects
    15. Summary
    16. Questions
  10. Chapter 5: Handling Errors
    1. Technical requirements
    2. Handling errors with return values
    3. Handling errors with exceptions
    4. Considering performance when using exceptions
    5. Retrying transient errors
    6. Understanding more advanced retrying
    7. Breaking circuits
    8. Designing exception class hierarchies
    9. Using core exception classes
    10. Summary
    11. 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
    4. Enforcing consistency with RuboCop
    5. Understanding the consequences of using arbitrary limits
    6. Checking basic code formatting with Ruby
    7. Realizing the actual importance of code formatting
    8. Summary
    9. Questions
  12. Section 2: Ruby Library Programming Principles
  13. Chapter 7: Designing Your Library
    1. Technical requirements
    2. Focusing on the user experience
    3. Library naming
    4. Library first impressions
    5. The simplest possible interface
    6. Determining the appropriate size for your library
    7. Handling complexity trade-offs during method design
    8. Fewer but more complex methods
    9. Summary
    10. Questions
  14. Chapter 8: Designing for Extensibility
    1. Technical requirements
    2. Using Ruby's extensibility features
    3. Designing plugin systems
    4. Designing a basic plugin system
    5. Handling changes to classes
    6. Plugin modifications to classes
    7. Supporting plugin dependencies
    8. Making plugin loading easier
    9. Handling subclasses in plugin systems
    10. Configuring plugins
    11. Understanding globally frozen, locally mutable design
    12. Summary
    13. 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
    3. Configuration DSLs
    4. DSLs for making specific changes
    5. DSLs for reducing the verbosity of code
    6. Libraries implemented as DSLs
    7. Implementing your DSL
    8. Learning when to use a DSL
    9. Summary
    10. 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
    5. Extracting a method
    6. Extracting a class
    7. Refactoring to add features
    8. Removing features properly
    9. Removing methods
    10. Removing constants
    11. Summary
    12. Questions
  19. Chapter 13: Using Common Design Patterns
    1. Technical requirements
    2. Learning about the many design patterns that are built into Ruby
    3. The object pool design pattern
    4. The prototype design pattern
    5. The private class data design pattern
    6. The proxy design pattern
    7. Handling cases where there can be only one
    8. Dealing with nothing
    9. Visiting objects
    10. Adapting and strategizing
    11. Summary
    12. 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
    5. Considerations when denormalizing your database design
    6. Other database design principles
    7. Treating the database as not just dumb storage
    8. Choosing the model layer
    9. Handling database and model errors
    10. Summary
    11. Further reading
    12. 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
    4. Ruby on Rails
    5. Sinatra
    6. Grape
    7. Roda
    8. Designing URL paths
    9. Structuring with monoliths, microservices, and island chains
    10. Summary
    11. 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
    6. Script injection
    7. SQL injection
    8. Code injection
    9. Approaching high-security environments
    10. Limiting database access
    11. Internal firewalling
    12. Randomizing memory layouts
    13. Limiting filesystem access
    14. Limiting system call access
    15. Summary
    16. 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
18.116.51.117