ruby attr_writer example

The attr_writer method in Ruby is used to define a writer method for the specified attribute. It creates a setter method that allows you to assign a value to the attribute from outside the class.

Here's an example of how attr_writer is used:

class Person
  attr_writer :name

  def initialize(name)
    @name = name
  end
end

person1 = Person.new("Alice")
person1.name = "Bob"

In this example, the attr_writer :name line creates a writer method for the name attribute of the Person class. The initialize method sets the initial value of @name when a new Person object is created. Finally, the person1.name = "Bob" line demonstrates how the attr_writer method allows the name attribute to be updated from outside the class.