如何从命令行启动一个ruby脚本?

时间:2022-09-21 01:11:52

On windows, I can run my ruby script like this:

在windows上,我可以像这样运行我的ruby脚本:

> ruby myscript.rb

but I want to set things up so that I can just do this instead?..

但是我想把事情安排好,这样我就可以这样做了。

> myscript.rb

How do I do this? I know it's possible because I've recently moved from one PC that had this set up to a new PC that doesn't (yet).

我该怎么做呢?我知道这是有可能的,因为我最近刚从一台电脑搬到一台新电脑上,而这台电脑现在还没有。

3 个解决方案

#1


24  

Associate the ".rb" file extension with the ruby interpreter. On Windows XP, one way to do this is to select "Tools|Folder options" in the file explorer, and to setup the association in the "File types" tab.

关联”。使用ruby解释器扩展的rb文件。在Windows XP上,一种方法是在file explorer中选择“Tools|文件夹选项”,并在“file types”选项卡中设置关联。

Another way would be to enter the following on the commandline which creates this file association for you:

另一种方法是在命令行上输入以下内容,为您创建这个文件关联:

assoc .rb=RubyScript
ftype RubyScript=ruby.exe %1 %*

#2


10  

Read the bottom part of Wikipedia Ruby.

阅读Wikipedia Ruby的底部部分。

Windows

窗户

If you install the native Windows version of Ruby using the Ruby One-Click Installer, then the installer has setup Windows to automatically recognize your Ruby scripts as executables. Just type the name of the script to run it.

如果您使用Ruby的一次单击安装程序安装本地Windows版本的Ruby,那么安装程序就会有安装窗口自动识别您的Ruby脚本作为可执行文件。只需输入运行它的脚本的名称。

$ hello-world.rb
Hello world

If this does not work, or if you installed Ruby in some other way, follow these steps.

如果这不起作用,或者您以其他方式安装Ruby,请遵循以下步骤。

1. Log in as an administrator.
2. Run the standard Windows "Command Prompt", cmd.
3. At the command prompt (i.e. shell prompt), run the following Windows commands. When you run ftype, change the command-line arguments to correctly point to where you installed the ruby.exe executable on your computer.

1。以管理员身份登录。2。运行标准的Windows“命令提示符”,cmd。3所示。在命令提示符(即shell提示符)中,运行以下Windows命令。当您运行ftype时,将命令行参数更改为正确地指向您安装ruby的位置。在你的电脑上执行可执行文件。

$ assoc .rb=RubyScript
.rb=RubyScript

$ ftype RubyScript="c:\ruby\bin\ruby.exe" "%1" %*
RubyScript="c:\ruby\bin\ruby.exe" "%1" %*

For more help with these commands, run "help assoc" and "help ftype".

要获得这些命令的更多帮助,请运行“help assoc”和“help ftype”。

#3


0  

None of the command line solutions worked for me on Windows 10. I've previously used the Windows GUI to open .rb files using Notepad++. When I run

在Windows 10上,没有一个命令行解决方案适合我。我以前使用Windows GUI来使用Notepad++打开.rb文件。当我运行

script.rb

script.rb

in the command line, it just opens Notepad++ with the .rb file (even after the command line fixes).

在命令行中,它只是用.rb文件(即使在命令行修复之后)打开Notepad++ +。

What solved the issue for me was

什么解决了我的问题是

  1. Right click a .rb file
  2. 右键单击a .rb文件
  3. Click "Open With"
  4. 点击“打开”
  5. Click "Choose another app"
  6. 点击“选择另一个应用程序”
  7. Click "More apps"
  8. 点击“应用”
  9. Click "Look for another app on this PC"
  10. 点击“在这台电脑上寻找另一个应用”
  11. Open the ruby.exe interpreter from my ruby installation.
  12. 打开ruby。来自ruby安装的exe解释器。

#1


24  

Associate the ".rb" file extension with the ruby interpreter. On Windows XP, one way to do this is to select "Tools|Folder options" in the file explorer, and to setup the association in the "File types" tab.

关联”。使用ruby解释器扩展的rb文件。在Windows XP上,一种方法是在file explorer中选择“Tools|文件夹选项”,并在“file types”选项卡中设置关联。

Another way would be to enter the following on the commandline which creates this file association for you:

另一种方法是在命令行上输入以下内容,为您创建这个文件关联:

assoc .rb=RubyScript
ftype RubyScript=ruby.exe %1 %*

#2


10  

Read the bottom part of Wikipedia Ruby.

阅读Wikipedia Ruby的底部部分。

Windows

窗户

If you install the native Windows version of Ruby using the Ruby One-Click Installer, then the installer has setup Windows to automatically recognize your Ruby scripts as executables. Just type the name of the script to run it.

如果您使用Ruby的一次单击安装程序安装本地Windows版本的Ruby,那么安装程序就会有安装窗口自动识别您的Ruby脚本作为可执行文件。只需输入运行它的脚本的名称。

$ hello-world.rb
Hello world

If this does not work, or if you installed Ruby in some other way, follow these steps.

如果这不起作用,或者您以其他方式安装Ruby,请遵循以下步骤。

1. Log in as an administrator.
2. Run the standard Windows "Command Prompt", cmd.
3. At the command prompt (i.e. shell prompt), run the following Windows commands. When you run ftype, change the command-line arguments to correctly point to where you installed the ruby.exe executable on your computer.

1。以管理员身份登录。2。运行标准的Windows“命令提示符”,cmd。3所示。在命令提示符(即shell提示符)中,运行以下Windows命令。当您运行ftype时,将命令行参数更改为正确地指向您安装ruby的位置。在你的电脑上执行可执行文件。

$ assoc .rb=RubyScript
.rb=RubyScript

$ ftype RubyScript="c:\ruby\bin\ruby.exe" "%1" %*
RubyScript="c:\ruby\bin\ruby.exe" "%1" %*

For more help with these commands, run "help assoc" and "help ftype".

要获得这些命令的更多帮助,请运行“help assoc”和“help ftype”。

#3


0  

None of the command line solutions worked for me on Windows 10. I've previously used the Windows GUI to open .rb files using Notepad++. When I run

在Windows 10上,没有一个命令行解决方案适合我。我以前使用Windows GUI来使用Notepad++打开.rb文件。当我运行

script.rb

script.rb

in the command line, it just opens Notepad++ with the .rb file (even after the command line fixes).

在命令行中,它只是用.rb文件(即使在命令行修复之后)打开Notepad++ +。

What solved the issue for me was

什么解决了我的问题是

  1. Right click a .rb file
  2. 右键单击a .rb文件
  3. Click "Open With"
  4. 点击“打开”
  5. Click "Choose another app"
  6. 点击“选择另一个应用程序”
  7. Click "More apps"
  8. 点击“应用”
  9. Click "Look for another app on this PC"
  10. 点击“在这台电脑上寻找另一个应用”
  11. Open the ruby.exe interpreter from my ruby installation.
  12. 打开ruby。来自ruby安装的exe解释器。