用文本IO双击可执行文件制作一个ruby脚本? [英] Make a ruby script with text IO double clickable executable file?

查看:60
本文介绍了用文本IO双击可执行文件制作一个ruby脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ruby的初学者,我不知道该怎么做...。

I'm a beginner at Ruby and I don't know how to do this....

现在我有了脚本,它可以完美地在终端,但我希望能够在桌面上创建它的可双击版本,然后在终端中打开一个窗口并运行脚本。 Automator和Platypus不起作用。我不确定该怎么办。

Right now I have the script and it runs perfectly in terminal but I was hoping to be able to create a double clickable version of it on the desktop that would then open a window in terminal and run the script. Automator and Platypus don't work. I'm not sure what to do. Thank you in advance.

推荐答案

假设您的目标是Mac OS X(Unix)或Linux,则可以在使其与终端兼容的

Assuming you're targeting Mac OS X (Unix) or Linux you can put this line at the top of your Ruby file to make it compatible with the Terminal:

#!/bin/ruby

基本上,这告诉外壳程序(终端)使用您在之后编写的任何可执行文件运行文件的内容#!。在这种情况下,您指示它使用 ruby​​ 可执行文件,该可执行文件用于在其他地方运行代码。

Basically, this tells the shell (Terminal) to run the contents of the file with whatever executable you write after #!. In this case, you're instructing it to use the ruby executable that you use to run your code elsewhere.

要允许执行Ruby脚本,您需要通过运行以下命令使其可执行:

To allow the Ruby script to be executed, you'll need to make it executable by running:

chmod a+x my_script.rb 

最后,要获得双击支持,您可能必须替换 .rb 扩展名为 .sh ,因此操作系统将其识别为Shell脚本而不是Ruby文件。

Finally, to get double clicking support, you might have to replace the .rb extension with .sh so the operating system recognizes it as a shell script and not a Ruby file.

您也可以考虑使用某种GUI。 鞋子是创建漂亮的用户界面的好方法,而且非常易于使用。如果我没有记错的话,它还允许您为Windows,OS X和Linux生成可执行文件。

You might also consider a GUI of some sort. Shoes is a great way to create nice user interfaces, and it's really easy to use. It also allows you to generate executable files for Windows, OS X, and Linux if I remember correctly.

下面是一个示例,它创建了一个可单击的基本按钮:

Here's an example, which creates a basic, clickable button:

Shoes.app do
  @button = button "Click Me"

  @button.click do
    puts "Clicked!"
  end
end

这篇关于用文本IO双击可执行文件制作一个ruby脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆