What is the difference between using @name
and name
(with an attr_reader
)? They use different path ways: @name
is direct while name
uses a method to reach @name
. But besides that, is there any difference? The outcome is the same.
Asked
Active
Viewed 199 times
0

sawa
- 165,429
- 45
- 277
- 381

Bri Expost
- 101
- 8
1 Answers
2
But besides that is there any difference because the outcome is the same.
Besides that? Not much. It's just that attr_reader
gives you a method (as you already know), so you can do all the things you do to methods: make public/private, decorate, override in a child class, include from a module, etc.
Oh, and also instance variables are harder to access from outside the class. Which is why attr_reader
and related helpers exist in the first place (to easily wrap ivars in public methods).

Sergio Tulentsev
- 226,338
- 43
- 373
- 367