Could someone explain what is going on with the ruby code below?

I’m just not understanding the documentation or how I can access the name instance variable of a class.

class Person
  attr_reader :name
  attr_writer :name
  def initialize(name)
    @name = name
  end
end

These are attribute accessors. Otherwise, you wouldn’t be able to read or change name directly.
For example, if you remove attr_reader and then try to print x.name, you will get an error.

1 Like