From: Gavin Kistner Date: 2005-06-02T23:46:50+09:00 Subject: Re: Implementing a Read-Only array --Apple-Mail-1--170027547 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed On Jun 2, 2005, at 3:55 AM, Robert Klemme wrote: > You don't even need the variable and the ugly concat > [...] Prettier, to be sure. Now I'm bothered by the fact that my delegate array thinks that it responds to methods which only exist to raise an error. I've hacked the delegate.rb code to add an #undelegate_method method. Comments on the code I used (and a cleaner way to do it) would be appreciated. Does this (feature, if not implementation) seem like it might be a good addition to the standard lib code? [Slim:~/Desktop] gavinkis% diff /usr/local/lib/ruby/1.8/delegate.rb / usr/local/src/ruby-1.8.2/lib/delegate.rb 100,110d99 < def self._ignored_methods < @_ignored_methods ||= {} < end < def self.undelegate_methods( *methods ) < ignore = _ignored_methods < methods.flatten.each do |m| < m = m.to_s.intern < undef_method( m ) < ignore[ m ] = true < end < end 115d103 < super if self.class._ignored_methods[ m ] #raise StandardError, "No such method: #{name}" [Slim:~/Desktop] gavinkis% cat readonlyarray.rb require 'delegate' class ReadOnlyArray < DelegateClass( Array ) undelegate_methods( %w{ << []= clear concat delete delete_at delete_if fill flatten! insert map! pop push reject! replace reverse! shift slice! sort! uniq! unshift } ) end a = [1,2] ro = ReadOnlyArray.new( a ) a << 3 puts a.respond_to?( :concat ) puts ro.respond_to?( :concat ) a.concat( [4,5,6] ) ro.concat( [7,8,9] ) [Slim:~/Desktop] gavinkis% ruby readonlyarray.rb true false /usr/local/lib/ruby/1.8/delegate.rb:115:in `method_missing': undefined method `concat' for [1, 2, 3, 4, 5, 6]:ReadOnlyArray (NoMethodError) from readonlyarray.rb:17 --Apple-Mail-1--170027547--