Use attr_accessor to add a new attribute and set it : attr_accessor « Class « Ruby






Use attr_accessor to add a new attribute and set it



class Employee
      attr_accessor :name
      def initialize
            @name = "" 
      end
end

employee1 = Employee.new
employee1.name = "Aneesha"

puts employee1.name

 








Related examples in the same category

1.Creating Readable and Writable Attributes: use attr_accessor
2.attr_accessor add accessor to a class
3.Get new defined instance methods
4.A logger