M
me
Hi,
How can I redefine Array#[] using an included Module? Here is my
attempt:
module AbsenceHandling
def if_absent_call(p)
@if_absent = p
self
end
def [](index)
result = old_accessor(index)
if(result.nil? && !@if_absent.nil?)
result = @if_absent.call(index)
self[index]=result
end
result
end
end
class Array
alias ld_accessor :[]
include AbsenceHandling
end
x = [1,2,3].if_absent_call(proc{|index|
"#{index}"
})
puts x[5]
Thanks very much,
Mike
How can I redefine Array#[] using an included Module? Here is my
attempt:
module AbsenceHandling
def if_absent_call(p)
@if_absent = p
self
end
def [](index)
result = old_accessor(index)
if(result.nil? && !@if_absent.nil?)
result = @if_absent.call(index)
self[index]=result
end
result
end
end
class Array
alias ld_accessor :[]
include AbsenceHandling
end
x = [1,2,3].if_absent_call(proc{|index|
"#{index}"
})
puts x[5]
Thanks very much,
Mike