Ruby - Using attr_accessor in class hierarchy

Description

Using attr_accessor in class hierarchy

Demo

class Thing 

    attr_reader :description # ww w  .  j  av a2  s .c  o m
    attr_writer :description 
    attr_writer :name 
                            
    def initialize( aName, aDescription )             
        @name         = aName 
        @description  = aDescription 
    end 

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

end    
class Product < Thing 
    attr_accessor :value 
                 
    def initialize( aName, aDescription ) 
        super( aName, aDescription ) 
    end                  
end 

t1 = Product.new( "ring", "a gift" ) 
t1.value = 800 
puts "This is Product1: #{t1.inspect}"

Result

The only indication that @value exists at all is this accessor definition:

attr_accessor :value 

Related Topic