© Carleton DiLeo 2019
C. DiLeoClean Rubyhttps://doi.org/10.1007/978-1-4842-5546-9_5

5. Classes

Carleton DiLeo1 
(1)
Boulder, CO, USA
 

In this chapter, we cover several techniques you can use to make high-quality classes.

Initialize Method

When instantiating a class in Ruby, the new class method calls the initialize instance method. Any parameters passed to the new method will be passed to the initialize method. The initialize method is a setup method for a class. It’s called before any other method defined in the class. If we have steps to perform before using the class, we place them in the initialize method. Let’s look at techniques we can use to improve our initialize methods.

Keep It Simple

The initialize method should be simple, which means limiting ...

Get Clean Ruby: A Guide to Crafting Better Code for Rubyists now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.