Ruby Programming: Attributes and accessors

12m 32s

Huw Collingbourne

Huw Collingbourne is a professional programmer, technology writer and instructor. He is Director of Technology at SapphireSteel Software (,... Read more

Attributes are the Ruby equivalents of what many other languages call ‘properties’ – they provide a convenient way of accessing variables in an object using getter and setter methods.

Ruby attributes let you access instance variables using a simple assignment and retrieval syntax that more closely resembles the syntax for assigning and retrieving the value of a variable than for calling methods and passing arguments to them. You can create assessors in a variety of ways – if you like you may write ‘accessor methods’ to read and write values. Alternatively, you can ask Ruby to create accessors automatically by specifying the names you want to use and passing those names as ‘symbols’ to attr_reader, attr_writer or attr_accessor. This lesson explains all these ways of creating accessors and it shows how you may selectively restrict either the read or write access to instance variables.