Ruby Design Patterns - Methods over Attributes
The objective of this article is to highlight the subtle differences between using class attributes and class methods in Ruby. Both offer a valid way to manipulate the state of an instance of a class. Things can get increasingly complex, hard to test and hard to maintain as more instance variables are introduced. Beginner and intermediate Rubyists often miss this subtle but important point which can introduce bugs that may be hard to fix in their native habitat.
The reasons I prefer to use methods over instance variables:
- Increases predictability of method calls
- Increases predictability when testing