当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby Fiber.backtrace用法及代码示例


本文简要介绍ruby语言中 Fiber.backtrace 的用法。

用法

backtrace → array
backtrace(start) → array
backtrace(start, count) → array
backtrace(start..end) → array

返回光纤的当前执行堆栈。 startcountend 允许仅选择部分回溯。

def level3
  Fiber.yield
end

def level2
  level3
end

def level1
  level2
end

f = Fiber.new { level1 }

# It is empty before the fiber started
f.backtrace
#=> []

f.resume

f.backtrace
#=> ["test.rb:2:in `yield'", "test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'", "test.rb:13:in `block in <main>'"]
p f.backtrace(1) # start from the item 1
#=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'", "test.rb:13:in `block in <main>'"]
p f.backtrace(2, 2) # start from item 2, take 2
#=> ["test.rb:6:in `level2'", "test.rb:10:in `level1'"]
p f.backtrace(1..3) # take items from 1 to 3
#=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'"]

f.resume

# It is nil after the fiber is finished
f.backtrace
#=> nil

相关用法


注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Fiber.backtrace。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。