How to delete attr_accessor in ruby

ⅰ亾dé卋堺 提交于 2019-12-08 08:28:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!