Rails 怪异的问题:rails console 中 tab 触发 IRB_EXIT

fsword · 2012年03月05日 · 最后由 fsword 回复于 2012年12月12日 · 2533 次阅读

现象是 rails console 里面 tab 的时候会触发 IRB_EXIT,让人很恼火,错误信息如下:

.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:83:in `throw': uncaught throw :IRB_EXIT (ArgumentError)
    from /home/john/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:83:in `irb_exit'
    from /home/john/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/context.rb:265:in `exit'
    from /home/john/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/extend-command.rb:23:in `irb_exit'
    from /home/john/.rvm/gems/ruby-1.9.2-p290/gems/simplecov-0.6.0/lib/simplecov/defaults.rb:52:in `block in <top (required)>'
/home/john/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/completion.rb:161:in `block (2 levels) in <module:InputCompletor>': can't convert Regexp to String (TypeError)

看起来是 completion.rb 的问题,源码如下:

ObjectSpace.each_object(Module){|m|
  begin
    name = m.name
  rescue Exception
    name = ""
  end
  next if name != "IRB::Context" and
    /^(IRB|SLex|RubyLex|RubyToken)/ =~ name #  <- Line 161
  candidates.concat m.instance_methods(false).collect{|x| x.to_s}
}
candidates.sort!
candidates.uniq!

我将 name 改为 name.to_s,问题可以解决,但是不明白为什么会有这个 bug,而且似乎在 irb 中没有这个现象,有人遇到过类似的情况么?

从未遇到

sorry. 点错了。

debug 进去看吧,那个 name 类型怎么可能变成 Regexp 呢。你这个抛出了堆栈还好弄,我遇到过两个没抛堆栈的问题,把我调死了。 现在我只求一些 gem 包,接到了异常可以给我抛出来,别接了啥也不说。

翻出了这个老帖子,有点好奇现在的做法,结果看到是直接包了一个 rescue 了事,各种无语

ObjectSpace.each_object(Module){|m|
  begin
    name = m.name
  rescue Exception
    name = ""
  end
  begin
    next if name != "IRB::Context" and
      /^(IRB|SLex|RubyLex|RubyToken)/ =~ name
  rescue Exception
    next
  end
  candidates.concat m.instance_methods(false).collect{|x| x.to_s}
}

需要 登录 后方可回复, 如果你还没有账号请 注册新账号