class Food
attr_accessor :protein
def initialize(protein)
@protein = protein
end
end
# What is attr_accessor, attr_reader or attr_writer in Ruby?
# Verbose (hand written reader and writer)
class Person
def name
@name
end
def name=(str)
@name = str
end
end
# or using attr_reader and/or attr_writer
class Person
attr_reader :name
attr_writer :name
end
# or using attr_accessor
class Person
attr_accessor :name
end
# Usage
person = Person.new
person.name = 'Dennis'
person.name # => "Dennis"