【问题标题】:Running Ruby scripts in Atom or command shell在 Atom 或命令 shell 中运行 Ruby 脚本
【发布时间】:2016-07-18 18:39:04
【问题描述】:

在这里完成 Ruby 业余爱好者。在各种交互式在线编码环境中使用 Ruby 后,我想我会尝试在我的 Windows 上设置 Ruby,就像我设置 Python 一样(使用 Atom 和 Terminal-Plus)。在过去 3 小时感到沮丧和谷歌搜索答案之后,我想我会把我的问题带到这里。

使用 Python,我可以将文件 (test.py) 保存在 Atom 中,然后使用 Terminal-Plus 通过键入以下命令执行它:

py -i test.py

这将创建一个交互式 shell,我可以在其中调用存储在我的测试 Python 脚本中的任何函数。我了解到我可以使用 Ruby(对于测试文件 test.rb)做类似的事情:

ruby -r test.rb

但这会产生以下错误:

C:/Ruby21-  x64/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such
 file -- test.rb (LoadError)
    from C:/Ruby21-x64/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'

如果我使用 cmd 提示符尝试执行脚本,也会发生同样的事情。我见过类似这样的其他问题,其中该人缺少他们试图调用的关键 RubyGem,但我的 test.rb 文件实际上是:

def xyz
  for i in 1..100
      puts i
  end
end

我只是想调用一个 Ruby 脚本并能够调用我在控制台中的脚本中存储的函数,在本例中为 xyz。

【问题讨论】:

    标签: ruby terminal console atom-editor


    【解决方案1】:

    -r命令行开关用于use require to load lib as a library before executing

    试试

    ruby test.rb
    

    从这个文件所在的目录中,它应该可以工作。

    【讨论】:

    • 使用 ruby​​ test.rb 似乎可以编译代码,但不会让我的终端处于可以与代码交互或调用我定义的函数的状态。例如,当我输入 ruby​​ test.rb 时,我的环境不会变成交互式 ruby​​ 环境。
    • 要运行带有预加载 ruby​​ 代码的 shell,请使用 irb -r test.rb
    猜你喜欢
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 2019-09-02
    • 2015-01-10
    相关资源
    最近更新 更多