The attr_accessor allows you to do this:
class Person attr_accessor :name, :age # from ww w . j av a2s . co m end x = Person.new x.name = "Fred" x.age = 10 puts x.name, x.age
attr_accessor simply writes some code for you.
This code is equivalent to the single attr_accessor :name, :age line in the preceding Person class:
class Person def name @name end def name=(name) @name = name end def age @age end def age=(age) @age = age end end
Here, this code defines the name and age methods that return the current object variables for those attributes.
It defines two "setter" methods that assign the values to the @name and @age object variables.