Hacker News new | ask | show | jobs
by lulin 5590 days ago
As Ruby instance variables are private, you need a getter to read them. Doing it this way (making everything private, but giving you simple methods for creating getters and setters) gives you the best of both worlds: Encapsulation and simple and concise code. attr_accessor simply creates the boilerplate code you would write yourself or use an IDE for in other languages. If you want to change a getter, just write a new method with the name of the variable and do whatever you want. This makes it easy to change your class logic later, but won't fill your classes with generic getter/setter code.