attr_reader creates one or more instance variables, with corresponding methods that return (get) the values of each method.
# attr_writer method automatically creates one or more instance variables, with corresponding methods that set the values of each method.
class Dog
attr_reader :bark
attr_writer :bark
end
dog = Dog.new
dog.bark="Woof!"
puts dog.bark # => Woof!
dog.instance_variables.sort # => ["@bark"]
Dog.instance_methods.sort - Object.instance_methods # => [ "bark", "bark=" ]
Related examples in the same category