Logan Capaldo
10/1/2007 8:31:00 PM
On 9/30/07, Rassat Nicolas <nicolas.rassat@free.fr> wrote:
> I have a class which inherit from Array. I want to perform some task
> each time something is append to an instance of this class. The problem
> is: there are so many way to append something to an array. For example:
>
class Narray
def initialize(a)
@array = a.dup.freeze
end
def method_missing(name, *args, &block)
@array.send(name, *args, &block)
rescue NoMethodError
super
rescue TypeError => e
if e.message =~ /can't modify frozen array/
a = @array.dup
p "#{name}ing"
r = a.send(name, *args, &block)
@array = a.freeze
r
else
raise e
end
end
def inspect
@array.inspect
end
def to_s
@array.to_s
end
end
narray = Narray.new [1,2,3]
narray.append 4
p narray
narray << 3
p narray
narray.fill(2)
p narray
Totally didn't test this