From: Gavin Kistner Date: 2005-06-01T23:00:58+09:00 Subject: Re: Implementing a Read-Only array --Apple-Mail-10--259184887 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=ISO-8859-1; delsp=yes; format=flowed On Jun 1, 2005, at 7:49 AM, Brian Schr=F6der wrote: > On 01/06/05, Gavin Kistner wrote: >> class ReadOnlyArray < Array >> alias_method :'__ro_<<', :'<<' #:nodoc: >> alias_method :__ro_insert, :insert #:nodoc: >> alias_method :__ro_delete_at, :delete_at #:nodoc: >> affectors =3D %w| << []=3D clear concat delete delete_at = delete_if >> fill flatten! insert map! pack pop push reject! replace reverse! >> shift slice! sort! uniq! unshift | >> affectors.each{ |name| undef_method name } >> end > Why not create a proxy class that contains an array and only forwards > the reading methods. It seems to me, that this would achieve exactly > what you want. That's an interesting idea. What advantage do you think that would =20 offer compared to my implementation above? How would you modify the =20 internal representation when necessary? (Perhaps the constructor for =20 the proxy class receives the array that it is supposed to wrap?) One advantage of mine versus the proxy approach would be that by =20 actually inheriting from Array, my class supports additional methods =20 defined by the user for Array or Enumerable. (If the user has added a =20= special Array#my_collect method, my array is extended as well.) (I think that it's correct that it would not be possible for a custom =20= user method to modify the array, if I'm undef'ing all core methods =20 for modifying it, yes?) --Apple-Mail-10--259184887--