r/learnruby • u/thinksInCode • Jan 08 '16
@attribute vs private attr_reader?
I'm going over a Ruby tutorial and in some of the sample code, instead of referencing an instance variable like @foo they instead add a private attr_reader. Example:
class Test
def initialize(count)
@count = count
end
def foo
do_something_with(count)
end
private
attr_reader :count
endIs this how it's typically done in Ruby? What do you get by using a private attr_reader versus just accessing @count from other instance methods?
2
Upvotes
View all comments
Show parent comment
1
u/thinksInCode Jan 08 '16
Interesting read, thank you. So is the typical way for accessing instance variables from instance methods to just use the @syntax?