Instance variables can always be read/written inside instance methods, which your code demonstrates. attr_accessor
makes instance variables readable/writable outside the class (by defining accessor methods). By adding it to your second example, you allow the following:
cat = Cat.new("Garfield")
puts cat.name
cat.name = "Maru"
which would raise NoMethodError
in your first example.