我有一些运行IRB.start的代码(从gem里面)

但是,我在magic-file.rb中收到此错误“没有这样的文件或目录@ rb_sysopen-控制台”

/Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb/magic-file.rb:7:in `initialize': No such file or directory @ rb_sysopen - console (Errno::ENOENT)
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb/magic-file.rb:7:in `open'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb/magic-file.rb:7:in `open'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb/input-method.rb:100:in `initialize'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb/context.rb:84:in `new'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb/context.rb:84:in `initialize'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb.rb:426:in `new'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb.rb:426:in `initialize'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb.rb:381:in `new'
    from /Users/you/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/irb.rb:381:in `start'
    from /Users/you/projects/mygem/lib/mygem/commands.rb:20:in `console'

最佳答案

我想到了!通过查看代码:

  • https://github.com/ruby/ruby/blob/trunk/lib/irb.rb
  • https://github.com/ruby/ruby/tree/trunk/lib/irb/

  • 我能够确定IRB检查ARGV,以便您可以在命令行上将IRB args传递给您在其中调用IRB.start的任何脚本。

    但是由于我试图创建一个rake任务来启动一个控制台,并且为了方便起见,我的库已经预加载了,所以我的命令行是:
    rake console
    

    而且IRB试图加载文件“控制台”,因为它假定第一个不带连字符的arg是要运行的脚本。有点烦人。 (IRB.start应该将args用作arg方法,以避免这种紧密耦合。)

    我设法通过在IRB.start之前添加以下内容来解决此问题:
    ARGV.clear
    

    更新:自被请求以来,这里正是使用ARGV的地方:
  • https://github.com/ruby/ruby/blob/trunk/lib/irb/init.rb#L124
  • 关于ruby - IRB.start没有开始,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33070092/

    10-13 04:42