Monday 30 May 2016

Getter and Setter Methods



In my previous posts you learn about the class and inheritance
Now we are going to get the information regarding the setter and getter methods of a ruby class

    class Person  
      def initialize(name)  
        self.name = name  
      end  
      def name  
        @name  
      end  
      def name=(name)  
        @name = name  
      end  
    end  
      
    person = Person.new("Andreas")  
    puts person.name  
    person.name = "David"  
    puts person.name  


In the above person class name definition is the one contains setter and getter methods.

In next post we will learn what attr_accessor.

No comments:

Post a Comment

My Experience in RUBYCONF INDIA 2016

Hi Readers, Rubyconf India is a global event complementing other RubyConf events across the world. I attended this event held on 19-20 ma...