James,

It appears, also, that changing @dependencies to a constant also does
not help.  Strange.

Ralph


Friday, November 6, 2009, 6:50:30 AM, you wrote:

JF> Hi,

JF> Is there any way of providing read only access to an array? (problem shown by code below).

JF> class A

JF>   def initialize
JF>     @dependencies = []
JF>   end

JF>   # intended to be read only access
JF>   def dependencies
JF>     @dependencies
JF>   end

JF>   def addDependency(d)
JF>     @dependencies << d
JF>     puts "adding #{d}"
JF>   end
JF> end


JF> a = A.new
JF> a.addDependency("foo")
JF> a.dependencies << "bar" # encapsulation subverted

JF> puts a.dependencies   # foo and bar both in array


JF> Any suggestions appreciated,
JF> James




-- 
Best regards,
 Ralph                            mailto:ralphs / dos32.com