Ruby/Reflection/caller

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Generating and Understanding Tracebacks

/usr/bin/ruby                                                  
 
def do_work
  first = true                                                 
  caller.each do |c|                                           
    puts %{#{(first ? "I" : " which")} was called by "#{c}"}   
    first = false                                              
  end                                                          
end                                                            
do_work



Get caller line number

def func1
  puts caller[0]
end
def func2
  func1
end
func2              # Prints: somefile.rb:6:in "func2"