Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In Ruby, you can't[0] access an instance variable without an accessor; they're private by default, with all that implies. Also, when you use attr_accessor, all it does is literally[1] create a getter and a setter method. Adding logic to them later is a simple matter of replacing the attr_accessor shortcut with a method with the right name. None of the client code has to change, and that's the important part.

[0] Not quite; see Object#instance_variable_get. But that's cheating, ugly, and generally frowned upon.

[1] Not quite, but the difference is an implementation detail.



Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: