问题
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
来源:https://stackoverflow.com/questions/18235690/how-to-delete-attr-accessor-in-ruby