Tuesday 31 May 2016

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

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

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