How do you view a sample of the call stack in ruby?

前端 未结 3 1657
一整个雨季
一整个雨季 2020-12-28 12:01

I\'m investigating different optimization techniques, and I came across this post Analyzing Code for Efficiency? by someone who believes that sampling the call stack is more

3条回答
  •  [愿得一人]
    2020-12-28 12:45

    You can throw an exception at any time, and then look at the $@ predefined variable, which returns an array of backtrace data. E.g. put this in foo.rb:

    begin                                                                        
      raise 'foo'                                                                
    rescue                                                                       
      puts $@                                                                    
    end  
    

    Then run it:

    $ ruby foo.rb 
    foo.rb:2:in `
    '

提交回复
热议问题