如何在终端中执行 Ruby 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8721369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:39:03  来源:igfitidea点击:

How to execute a Ruby script in Terminal?

rubyterminal

提问by Tom Maxwell

I've set everything up that I need on my Mac (Ruby, Rails, Homebrew, Git, etc), and I've even written a small program. Now, how do I execute it in Terminal? I wrote the program in Redcar and saved it as a .rb, but I don't know how to execute it through Terminal. I want to run the program and see if it actually works. How do I do this?

我已经在 Mac 上设置了我需要的一切(Ruby、Rails、Homebrew、Git 等),我什至编写了一个小程序。现在,我如何在终端中执行它?我在 Redcar 中编写了程序并将其保存为 .rb,但我不知道如何通过终端执行它。我想运行该程序,看看它是否真的有效。我该怎么做呢?

回答by Robin

Just call: ruby your_program.rb

只需致电: ruby your_program.rb

or

或者

  • start your program with #!/usr/bin/env ruby,
  • make your file executable by running chmod +x your_program.rb
  • and do ./your_program.rb some_param
  • 开始你的程序#!/usr/bin/env ruby
  • 通过运行使您的文件可执行 chmod +x your_program.rb
  • 并做 ./your_program.rb some_param

回答by vijay

Open your terminal and open folder where file is saved.
Ex /home/User1/program/test.rb

打开终端并打开保存文件的文件夹。
前任/home/User1/program/test.rb

  1. Open terminal
  2. cd /home/User1/program
  3. ruby test.rb
  1. 打开终端
  2. cd /home/User1/program
  3. ruby test.rb

format or test.rb

格式或 test.rb

class Test 
  def initialize
   puts "I love India"
  end
end

# initialize object
Test.new

output

输出

I love India

回答by Sergio Tulentsev

Assuming ruby interpreter is in your PATH (it should be), you simply run

假设 ruby​​ 解释器在您的 PATH 中(应该是),您只需运行

ruby your_file.rb

回答by Priti Biyani

To call ruby file use : ruby your_program.rb

调用 ruby​​ 文件使用: ruby your_program.rb

To execute your ruby file as script:

要将您的 ruby​​ 文件作为脚本执行:

  1. start your program with #!/usr/bin/env ruby

  2. run that script using ./your_program.rb param

  3. If you are not able to execute this script check permissions for file.
  1. 开始你的程序 #!/usr/bin/env ruby

  2. 使用运行该脚本 ./your_program.rb param

  3. 如果您无法执行此脚本,请检查文件的权限。

回答by Jokester

Just invoke ruby XXXXX.rbin terminal, if the interpreter is in your $PATH variable.

ruby XXXXX.rb如果解释器在您的 $PATH 变量中,只需在终端中调用。

( this can hardly be a rails thing, until you have it running. )

(这几乎不可能是 Rails 的事情,除非你让它运行。)

回答by sinister

Although its too late to answer this question, but still for those guys who came here to see the solution of same problem just like me and didn't get a satisfactory answer on this page, The reason is that you don't have your file in the form of .rb extension. You most probably have it in simple text mode. Let me elaborate. Binding up the whole solution on the page, here you go (assuming you filename is abc.rb or at least you created abc):

虽然现在回答这个问题已经太晚了,但是对于那些和我一样来这里看同样问题的解决方案却没有在这个页面上得到满意答案的人来说,原因是你没有你的文件以 .rb 扩展名的形式。您很可能在简单文本模式下使用它。让我详细说明一下。绑定页面上的整个解决方案,你去(假设你的文件名是 abc.rb 或者至少你创建了 abc):

Type in terminal window:

在终端窗口中输入:

cd ~/to/the/program/location
ruby abc.rb

and you are done

你就完成了

If the following error occurs

如果出现以下错误

ruby: No such file or directory -- abc.rb (LoadError)

Then go to the directory in which you have the abc file, rename it as abc.rb Close gedit and reopen the file abc.rb. Apply the same set of commands and success!

然后进入abc 文件所在的目录,将其重命名为abc.rb 关闭gedit 并重新打开文件abc.rb。应用相同的命令集并成功!

回答by Gokigooooks

For those not getting a solution for older answers, i simply put my file name as the very first line in my code.

对于那些没有得到旧答案解决方案的人,我只是将我的文件名作为代码中的第一行。

like so

像这样

 #ruby_file_name_here.rb

 puts "hello world"

回答by Nikhil Sahu

In case someone is trying to run a script in a RAILSenvironment, rails provide a runner to execute scripts in rails context via

如果有人试图在RAILS环境中运行脚本,rails 提供了一个运行器来通过以下方式在 rails 上下文中执行脚本

rails runner my_script.rb

rails runner my_script.rb

More details here: https://guides.rubyonrails.org/command_line.html#rails-runner

更多细节在这里:https: //guides.rubyonrails.org/command_line.html#rails-runner

回答by Akhilan

Open Terminal

打开终端

cd to/the/program/location
ruby program.rb

or add #!/usr/bin/env rubyin the first of your program (script tell that this is executed using Ruby Interpreter)

或者#!/usr/bin/env ruby在你的程序的第一个添加(脚本告诉这是使用 Ruby 解释器执行的)

Open Terminal

打开终端

cd to/the/program/location
chmod 777 program.rb
./program.rb