如何删除Ruby中的attr_accessor [英] How to delete attr_accessor in ruby
本文介绍了如何删除Ruby中的attr_accessor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在ruby中,如果我有一个类greet
并具有方法say_hi
,当name是类greet
的实例变量时,该方法打印出"Hello #{@name}"
,并且我允许通过添加@name
attr_accessor :name
,所以现在我可以直接更改@name
.
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
.
但是,如果我不再希望人们能够直接更改@name
,该如何删除该attr_accessor
?
But how do I remove this attr_accessor
if I no longer want people to be able to change @name
directly?
推荐答案
您不能拥有课程greet
,所以我想您还有其他内容,例如A
.
You cannot have a class greet
, so I suppose you have something else, say A
.
class A
undef :name
undef :name=
end
这篇关于如何删除Ruby中的attr_accessor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文