How to delete attr_accessor in ruby

2019-05-31 09:45发布

问题:

In ruby, if I have a class greet and has method say_hi that prints out "Hello #{@name}" when name is a instance variable of class greet, and I allow access to @name by adding in the attr_accessor :name, so now I can directly change @name.

But how do I remove this attr_accessor if I no longer want people to be able to change @name directly?

回答1:

You cannot have a class greet, so I suppose you have something else, say A.

class A
  undef :name
  undef :name=
end