Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

What Does attr_reader Do?

I didn't quite understand what this method(?) does. The Ruby Docs go right over my head. Can someone explain what this does?

Thanks in advance!

1 Answer

Milo Winningham
seal-mask
.a{fill-rule:evenodd;}techdegree
Milo Winningham
Web Development Techdegree Student 3,317 Points

attr_reader :attribute_name is basically shorthand to create a method that returns @attribute_name:

def attribute_name
  @attribute_name
end

attr_accessor does the same thing but also creates a method that can be used to set the instance variable. attr_accessor :attribute_name would create the same method shown above, plus this:

def attribute_name=(value)
  @attribute_name = value
end