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

时间:2022-05-17 00:01:04

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,但是我不知道如何通过Terminal执行它。我想运行这个程序,看看它是否有效。我该怎么做呢?

8 个解决方案

#1


192  

Just call: ruby your_program.rb

就叫:ruby your_program.rb

or

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

#2


21  

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

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

ruby your_file.rb

#3


19  

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. 打开终端
  3. cd /home/User1/program
  4. cd /home/User1/program
  5. ruby test.rb
  6. 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

#4


10  

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

    以#开始你的程序!/usr/bin/env红宝石

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

    使用./your_program运行该脚本。rb参数

  3. If you are not able to execute this script check permissions for file.
  4. 如果您无法执行此脚本,请检查文件的权限。

#5


4  

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

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

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

(这几乎不可能是rails的东西,除非你让它运行。)

#6


3  

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。应用相同的命令集并获得成功!

#7


3  

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"

#8


2  

Open Terminal

打开终端

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

or add #!/usr/bin/env ruby in 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

#1


192  

Just call: ruby your_program.rb

就叫:ruby your_program.rb

or

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

#2


21  

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

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

ruby your_file.rb

#3


19  

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. 打开终端
  3. cd /home/User1/program
  4. cd /home/User1/program
  5. ruby test.rb
  6. 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

#4


10  

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

    以#开始你的程序!/usr/bin/env红宝石

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

    使用./your_program运行该脚本。rb参数

  3. If you are not able to execute this script check permissions for file.
  4. 如果您无法执行此脚本,请检查文件的权限。

#5


4  

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

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

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

(这几乎不可能是rails的东西,除非你让它运行。)

#6


3  

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。应用相同的命令集并获得成功!

#7


3  

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"

#8


2  

Open Terminal

打开终端

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

or add #!/usr/bin/env ruby in 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