当我在命令行上使用 rdebug 选项运行它时,我的代码给了我一个分段错误:
font_size = {"ft0"=>26, "ft1"=>10, "ft2"=>8, "ft3"=>9, "ft4"=>9, "ft5"=>7, "ft6"=>8, "ft7"=>9, "ft8"=>7, "ft9"=>8, "ft10"=>9, "ft11"=>8}
classes = ["ft3", "ft10", "ft6", "ft9", "ft11", "ft4", "ft2", "ft12"]
large_class = classes.max{|a,b| font_size[a] <=> font_size[b] }
puts large_class.to_s
错误是:
test_segfault.rb:3: [BUG] Segmentation fault
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.3.0]
-- control frame ----------
c:0006 p:---- s:0016 b:0016 l:000011 d:000015 IFUNC
c:0005 p:---- s:0014 b:0014 l:000013 d:000013 CFUNC :each
c:0004 p:---- s:0012 b:0012 l:000011 d:000011 CFUNC :max
c:0003 p:0089 s:0009 b:0009 l:001588 d:000bc0 EVAL test_segfault.rb:3
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:0000 s:0002 b:0002 l:001588 d:001588 TOP
---------------------------
-- Ruby level backtrace information ----------------------------------------
test_segfault.rb:3:in `<main>'
test_segfault.rb:3:in `max'
test_segfault.rb:3:in `each'
-- C level backtrace information -------------------------------------------
[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html
Abort trap
当我在没有-rdebug
选项的情况下运行它时,我收到一个错误:
ruby test_segfault.rb
test_segfault.rb:3:in `each': comparison of String with String failed (ArgumentError)
from test_segfault.rb:3:in `max'
from test_segfault.rb:3:in `<main>'
如果我修改示例数据,则代码可以正常工作。