39,59 €
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.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 520
Veröffentlichungsjahr: 2021
Build better software with more intuitive, maintainable, scalable, and high-performance Ruby code
Jeremy Evans
BIRMINGHAM—MUMBAI
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: 2100821
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
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.
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.
The purpose of this book is to teach useful principles for intermediate to advanced Ruby programmers to follow. The focus is not generally on how to implement solutions, but on different implementation approaches, the trade-offs between them, and why some approaches are better in certain situations. While the main focus of the book is teaching principles, in some cases this book also teaches advanced Ruby programming techniques.
This book starts by teaching some fundamental principles, such as how best to use the core classes, when and how best to use each variable type, and how best to use the different types of method arguments. After building on the fundamental principles, the book teaches principles for better library design, such as how best to design extensible plugin systems, trade-offs when using metaprogramming and DSLs, and how best to approach testing, refactoring, and optimization. This book concludes with a few small chapters that are focused on principles specific to web programming in Ruby, with a separate chapter each on database design, application design, and web application security.
The target audience for the book is intermediate to advanced Ruby programmers who are interested in learning principles to improve their Ruby programming.
Chapter 1, Getting the Most out of Core Classes, focuses on the optimal usage of the built-in classes.
Chapter 2, Designing Useful Custom Classes, focuses on when it makes sense to implement a custom class, applying SOLID design to custom classes, and the trade-offs between having large classes and having a large number of classes.
Chapter 3, Proper Variable Usage, focuses on how best to use each of Ruby's variable types.
Chapter 4, Methods and Their Arguments, focuses on method naming principles, the best usage of each of the method argument types, and choosing proper method visibility.
Chapter 5, Handling Errors, focuses on the trade-offs between using exceptions and return values for handling errors, handling transient errors, and designing exception class hierarchies.
Chapter 6, Formatting Code for Easy Reading, focuses on different viewpoints on the importance of syntactic complexity and the downsides of arbitrary limits.
Chapter 7, Designing Your Library, focuses on designing your library around the user experience and complexity trade-offs when designing methods for your library.
Chapter 8, Designing for Extensibility, focuses on designing useful plugin systems to allow for extensibility in libraries.
Chapter 9, Metaprogramming and When to Use It, focuses on the pros and cons of abstraction, avoiding redundancy, and trade-offs between the two approaches Ruby has for metaprogramming.
Chapter 10, Designing Useful Domain-Specific Languages, focuses on when and how best to design DSLs.
Chapter 11, Testing to Ensure Your Code Works, focuses on why testing is so important, how to approach testing and manage complexity during testing, and the importance of code coverage.
Chapter 12, Handling Change, focuses on when and how best to implement refactoring in libraries, and deprecation strategies.
Chapter 13, Using Common Design Patterns, focuses on principles for the best usage of five common design patterns.
Chapter 14, Optimizing Your Library, focuses on determining when optimization is needed and how to approach optimization if it is needed.
Chapter 15, The Database Is Key, focuses on why database design is so important in web programming, how best to use database features, and how best to handle database errors.
Chapter 16, Web Application Design Principles, focuses on trade-offs for different types of application design, different frameworks, and different URL designs.
Chapter 17, Robust Web Application Security, focuses on important web security techniques and how to approach designing web applications for high-security environments.
This book assumes intermediate to advanced knowledge of the Ruby programming language. There are sections of the book that are accessible to those with only basic knowledge of Ruby, but most of the book assumes you already understand how Ruby works and tries to teach principles for more productive usage of Ruby.
While most of the ideas and principles discussed in the book, and most of the code examples used in the book, apply to any version of Ruby, some of the examples and principles are specific to Ruby 3.0, the latest release at the time of publication.
If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.
You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Polished-Ruby-Programming. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
There are a number of text conventions used throughout this book.
Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "As an example of this, consider a SQL database library that needs to execute INSERT, UPDATE, and DELETE SQL queries to modify data."
A block of code is set as follows:
class Foo
def self.bar
:baz
end
end
Any command-line input or output is written as follows:
# Warming up --------------------------------------
# MultiplyProf 28.531k i/100ms
# Calculating -------------------------------------
# MultiplyProf 284.095k (± 0.3%) i/s
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.
The objective of this section is for you to understand the fundamental principles and trade-offs involved in Ruby programming, at the level of individual classes and methods.
This section comprises the following chapters:
Chapter 1, Getting the Most out of Core ClassesChapter 2, Designing Useful Custom ClassesChapter 3, Proper Variable UsageChapter 4, Methods and Their ArgumentsChapter 5, Handling ErrorsChapter 6,Formatting Code for Easy Reading