Gavin Kistner
1/30/2007 5:30:00 PM
On Jan 30, 10:15 am, "Tim Pease" <tim.pe...@gmail.com> wrote:
>This is an utter kludge and I would not recommend using it in any
> production code, but here it is ... oh, and it will only work for this
> very specific case :/
[snip]
Very tricky!
Let's make it a little shorter and more robust:
class A
def say_hi
m = %r/([^:]+):(\d+)/.match caller.first
return if m.nil?
line = IO.readlines( m[1] )[ m[2].to_i - 1 ]
return if line.nil?
puts "Hi, I am #{line[/\S+(?=.say_hi)/] || '(unknown)'}"
end
end
a = A.new
b = a
c = [ a ]
d = { :foo=>c[0] }
a.say_hi
#=> Hi, I am a
b.say_hi
#=> Hi, I am b
c[0].say_hi
#=> Hi, I am c[0]
d[:foo].say_hi
#=> Hi, I am d[:foo]
x=0
y = c[x].say_hi
#=> Hi, I am c[x]
c[x].send( :say_hi )
#=> Hi, I am (unknown)