Ruby tutorial session 5

  1.  
  2. Attr_accessor:

     

    You can declare the attributes for the ruby class using the keyword 'attr_accessor'.

    While initializing object you can assign the value to it. Check the following example and practice it.

     

  3. class Person  
  4.   attr_accessor :name  
  5.     
  6.   def initialize(name)  
  7.     self.name = name  
  8.   end  
  9. end  
  10.   
  11. person = Person.new("Andreas")  
  12. puts person.name  
  13. person.name = "David"  
  14. puts person.name  

  15. Hope you will practice it, i will get back to you with new concept.

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...