对不起这个noob的问题…让我们说我们有:
class TestMe attr_reader :array def initialize @array = (1..10).to_a end
结束
那么可以做到:
>> a = TestMe.new => #<TestMe:0x00000005567228 @x=[1,2,3,4,5,6,7,8,9,10]> >> a.array.map! &:to_s => ["1","2","3","4","5","6","7","8","9","10"] >> a.array => ["1","10"]
>这显然是针对封装,不是吗?
有没有办法快速保护数组变量不被更改?
> …或者每次我的实例变量具有“破坏性”的方法时,是否需要实现深度拷贝阅读器?
编辑我读到某处它是“糟糕的OO”暴露数组实例变量.如果是真的,为什么?