类 Thread::Backtrace

回溯的内部表示。用户永远不会直接与该类的对象交互,但可以使用类方法获取当前会话的回溯设置。

公共类方法

Thread::Backtrace::limit → integer 点击切换源代码

返回由 --backtrace-limit 命令行选项设置的最大回溯长度。默认值为 -1,表示无限回溯。如果值为零或正数,则由 Exception#full_message 生成的错误回溯将被缩短,多余的行将被替换为 ... 3 levels...

$ ruby -r net/http -e "p Thread::Backtrace.limit; Net::HTTP.get(URI('http://wrong.address'))"
- 1
.../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': Failed to open TCP connection to wrong.address:80 (getaddrinfo: Name or service not known) (SocketError)
    from .../lib/ruby/3.1.0/socket.rb:227:in `foreach'
    from .../lib/ruby/3.1.0/socket.rb:632:in `tcp'
    from .../lib/ruby/3.1.0/net/http.rb:998:in `connect'
    from .../lib/ruby/3.1.0/net/http.rb:976:in `do_start'
    from .../lib/ruby/3.1.0/net/http.rb:965:in `start'
    from .../lib/ruby/3.1.0/net/http.rb:627:in `start'
    from .../lib/ruby/3.1.0/net/http.rb:503:in `get_response'
    from .../lib/ruby/3.1.0/net/http.rb:474:in `get'
.../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': getaddrinfo: Name or service not known (SocketError)
    from .../lib/ruby/3.1.0/socket.rb:227:in `foreach'
    from .../lib/ruby/3.1.0/socket.rb:632:in `tcp'
    from .../lib/ruby/3.1.0/net/http.rb:998:in `connect'
    from .../lib/ruby/3.1.0/net/http.rb:976:in `do_start'
    from .../lib/ruby/3.1.0/net/http.rb:965:in `start'
    from .../lib/ruby/3.1.0/net/http.rb:627:in `start'
    from .../lib/ruby/3.1.0/net/http.rb:503:in `get_response'
    from .../lib/ruby/3.1.0/net/http.rb:474:in `get'
    from -e:1:in `<main>'

$ ruby --backtrace-limit 2 -r net/http -e "p Thread::Backtrace.limit; Net::HTTP.get(URI('http://wrong.address'))"
2
.../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': Failed to open TCP connection to wrong.address:80 (getaddrinfo: Name or service not known) (SocketError)
    from .../lib/ruby/3.1.0/socket.rb:227:in `foreach'
    from .../lib/ruby/3.1.0/socket.rb:632:in `tcp'
     ... 7 levels...
.../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': getaddrinfo: Name or service not known (SocketError)
    from .../lib/ruby/3.1.0/socket.rb:227:in `foreach'
    from .../lib/ruby/3.1.0/socket.rb:632:in `tcp'
     ... 7 levels...

$ ruby --backtrace-limit 0 -r net/http -e "p Thread::Backtrace.limit; Net::HTTP.get(URI('http://wrong.address'))"
0
.../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': Failed to open TCP connection to wrong.address:80 (getaddrinfo: Name or service not known) (SocketError)
     ... 9 levels...
.../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': getaddrinfo: Name or service not known (SocketError)
     ... 9 levels...
static VALUE
backtrace_limit(VALUE self)
{
    return LONG2NUM(rb_backtrace_length_limit);
}