windows 如何使Ruby文件作为可执行文件运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5518297/
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
How to make Ruby file run as executable?
提问by Apoorv Saxena
I want my Ruby Script File to run as executable from any directory of Windows XP. I have created a test.rb (Ruby Script file) and want to run it from any directory of my Windows as "test" for example, "C:\test" or "C:\Directory\test" runs my file test.rb.
我希望我的 Ruby 脚本文件可以从 Windows XP 的任何目录中作为可执行文件运行。我创建了一个 test.rb(Ruby 脚本文件)并希望从我的 Windows 的任何目录运行它作为“测试”,例如,“C:\test”或“C:\Directory\test”运行我的文件测试。 rb.
#!/usr/bin/envy ruby
p "Hi this is my test file"
I have added the shebang code in my ruby file but when I have to run the Ruby Script, I have to locate my Script file and run it expicitly as "ruby test.rb".
我已经在我的 ruby 文件中添加了 shebang 代码,但是当我必须运行 Ruby 脚本时,我必须找到我的脚本文件并以“ruby test.rb”的形式明确运行它。
I have also made the file executable by executing the command:$ chmod +x hello-world.rb
, but it still does not work.
我还通过执行命令使文件可执行:$ chmod +x hello-world.rb
,但它仍然不起作用。
Thanks in advance.
提前致谢。
回答by Michelle Tilley
I assume you're using Linux or OS X and creating the file on a disk accessible from Windows? Windows does not use shebangs, and it does not use Unix file modes. You will need to associate files with the .rbextension to the Ruby executable; details for that operation can be found at this Stack Overflow question; once you have done this, you can run C:\whatever\test.rbor C:\whatever\testto execute the script.
我假设您使用的是 Linux 或 OS X 并在可从 Windows 访问的磁盘上创建文件?Windows 不使用 shebang,也不使用 Unix 文件模式。您需要将文件与.rbRuby 可执行文件的扩展名相关联;可以在此堆栈溢出问题中找到该操作的详细信息;完成此操作后,您可以运行C:\whatever\test.rb或C:\whatever\test执行脚本。

