R
Rassat Nicolas
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 < Array
alias_method
ld_push,
ush
def push(*obj)
p "pushing #{obj}"
old_push(*obj)
end
alias_method
ld_append, :<<
def <<(obj)
p "<<ing #{obj}"
old_append(obj)
end
alias_method
ld_bracket, :[]=
def []=(i,l=0)
p "[]=ing #{i}"
old_bracket(i,l)
end
alias_method
ld_add, :+
def +(i)
p "+ing #{i}"
old_add(i)
end
end
a = Narray.new
a.push(1)
a << 2
a[a.length] = 3
a = a + [4]
p a
I forgot Array#insert, Array#fill and probably other...
Is there any Array#append function that can hooked to perform task every
time something is append to a?
Any solution is welcome.
LarsTico
PS: sorry for my poor frenchy english, but I hope you understand me.
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 < Array
alias_method
def push(*obj)
p "pushing #{obj}"
old_push(*obj)
end
alias_method
def <<(obj)
p "<<ing #{obj}"
old_append(obj)
end
alias_method
def []=(i,l=0)
p "[]=ing #{i}"
old_bracket(i,l)
end
alias_method
def +(i)
p "+ing #{i}"
old_add(i)
end
end
a = Narray.new
a.push(1)
a << 2
a[a.length] = 3
a = a + [4]
p a
I forgot Array#insert, Array#fill and probably other...
Is there any Array#append function that can hooked to perform task every
time something is append to a?
Any solution is welcome.
LarsTico
PS: sorry for my poor frenchy english, but I hope you understand me.