Instance and Class Variable Accessor Methods

When you define classes in Ruby, you must specify what kind of access you want to give Instance and Class attributes/variables which are defined in that class.

The way you do it is that you write reader and writer methods. You can write these methods out explicitly or use the shorthand attribute (attr_) methods like attr_reader, attr_writer and attr_accessor. These are methods defined in core Ruby.

However, in Ruby, there is no equivalent attr methods to read and write Class variables, at least none that I know of. Luckily, they are defined in Rails though and are called cattr_reader, cattr_writer and cattr_accessor. Be sure to test it out using script/console. You won’t be able to try it out in irb since irb uses Ruby not the Rails libraries.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
Facebook Twitter Email

Leave a Reply