Table Of ContentPolished Ruby
Programming
Build better software with more intuitive,
maintainable, scalable, and high-performance
Ruby code
Jeremy Evans
BIRMINGHAM—MUMBAI
Polished Ruby Programming
Copyright © 2021 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system,
or transmitted in any form or by any means, without the prior written permission of the
publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers
and distributors, will be held liable for any damages caused or alleged to have been caused
directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
Group Product Manager: Aaron Lazar
Publishing Product Manager: Alok Dhuri
Senior Editor: Rohit Singh
Content Development Editor: Tiksha Lad
Technical Editor: Pradeep Sahu
Copy Editor: Safis Editing
Project Coordinator: Deeksha Thakkar
Proofreader: Safis Editing
Indexer: Pratik Shirodkar
Production Designer: Shankar Kalbhor
First published: June 2021
Production reference: 1110621
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-80107-272-4
www.packt.com
To Allyson, Jaden, Ruby, Jennifer, and Veronica, who most shaped my life.
- Jeremy Evans
Contributors
About the author
Jeremy Evans is a Ruby committer who focuses on fixing bugs in Ruby, as well as
improving the implementation of Ruby. He is the maintainer of many popular Ruby
libraries, including the fastest web framework (Roda) and fastest database library (Sequel).
His libraries are known not just for their performance, but also for their code quality,
understandability, documentation, and how quickly any bugs found are fixed. For his
contributions to Ruby and the Ruby community, he has received multiple awards, such
as receiving the prestigious RubyPrize in 2020 and being chosen as a Ruby Hero in 2015.
He has given presentations at over 20 Ruby conferences. In addition to working on Ruby,
he is also a committer for the OpenBSD operating system.
I would like to thank the editors, technical reviewers, and other Packt staff
for their help in creating this book. I would especially like to thank Janko
Marohnić for his reviews and recommendations, which resulted in so many
improvements to this book.
About the reviewers
Jagdish Narayandasani has more than 14 years of experience in software development
and almost a decade of experience in working on Ruby on Rails. He is currently working
as a Technology Specialist with a New York-based Fintech firm. He often contributes as a
echnical reviewer for technical books. He has worked on a variety of technologies, such as
Java, Ruby, Elixir, and the AWS cloud for different companies. He has worked in various
industries, such as finance, retail, healthcare, and wealth management. At the moment, he
is trying his hand at blockchain technology. In his free time, he loves to spend time with
his family, explore new places, and try new food.
Janko Marohnić is a seasoned Ruby developer with big love for open source. He has
authored many Ruby libraries, spoken at conferences, and writes on his blog about his
discoveries in the Ruby ecosystem (most of them are libraries created or maintained by
the author of this book). He values well-tested code, separation of responsibilities, and
doing more with less. Outside of programming, Janko enjoys dancing, acrobatic activities,
and playing the piano.
Table of Contents
Preface
Section 1:
Fundamental Ruby Programming Principles
1
Getting the Most out of Core Classes
Technical requirements 4 Learning how best to use
Learning when to use core classes 4 arrays, hashes, and sets 16
Best uses for true, false, and nil Implementing an in-memory database 16
objects 5
Working with Struct – one of
Different numeric types for the underappreciated core
different needs 8 classes 21
Understanding how symbols Summary 24
differ from strings 12
Questions 24
Further reading 24
2
Designing Useful Custom Classes
Technical requirements 26 The open-closed principle 32
Learning when to create a The Liskov substitution principle 35
custom class 26 The interface segregation principle 38
The dependency inversion principle 38
Handling trade-offs in SOLID
design 29
Deciding on larger classes or
The single-responsibility principle 29 more classes 42
Learning when to use custom Summary 48
data structures 46 Questions 48
3
Proper Variable Usage
Technical requirements 50 Understanding how constants
Using Ruby's favorite variable are just a type of variable 74
type – the local variable 50 Handling scope issues with constants 75
Increasing performance by adding Visibility differences between
local variables 50 constants and class instance variables 79
Avoiding unsafe optimizations 55 Naming considerations with constants 80
Handling scope gate issues 57
Replacing class variables 81
Naming considerations with local
Replacing class variables with constants 84
variables 61
Replacing class variables with class
Learning how best to use instance variables using the superclass
instance variables 65 lookup approach 86
Replacing class variables with class
Increasing performance with instance
instance variables using the copy to
variables 66
subclass approach 88
Handling scope issues with instance
variables 71
Avoiding global variables, most
Naming considerations for instance of the time 89
variables 73
Summary 92
Questions 92
Further reading 92
4
Methods and Their Arguments
Technical requirements 94 Using the many types of
Understanding that there are method arguments 100
no class methods, only instance Positional arguments 101
methods 94 Optional positional arguments 105
Naming methods 98 Rest arguments 108
Keyword arguments 112
Special method names 99
Block arguments 123
Learning about the importance Delegating to other objects 135
of method visibility 129
Summary 138
Fixing visibility mistakes 131
Questions 138
Handling delegation 133
5
Handling Errors
Technical requirements 140 Breaking circuits 159
Handling errors with return
Designing exception class
values 140
hierarchies 161
Handling errors with exceptions 145
Using core exception classes 164
Considering performance when using
Summary 165
exceptions 149
Questions 165
Retrying transient errors 151
Understanding more advanced retrying 156
6
Formatting Code for Easy Reading
Technical requirements 168 Understanding the
Recognizing different consequences of using arbitrary
perspectives of code formatting 168 limits 173
Learning how syntactic Checking basic code formatting
consistency affects with Ruby 177
maintainability 170 Realizing the actual importance
of code formatting 179
Enforcing consistency with RuboCop 172
Summary 179
Questions 180