Ruby - Class Attribute accessors

Introduction

Attribute accessors can initialize more than one attribute at a time:

attr_reader :name, :description 
attr_writer(:name, :description) 
attr_accessor(:value, :id, :owner) 

Parentheses around the arguments are optional but preferred.

class Thing 

    attr_reader :name, :description 
    attr_writer(:name, :description) 
    attr_accessor(:value, :id, :owner) 
                            
    def initialize( aName, aDescription )             
        @name         = aName 
        @description  = aDescription 
    end 

    # get accessor for @name 
    def name 
        return @name.capitalize 
    end 

end    

Related Topic