Make a ruby script with text IO double clickable executable file?

后端 未结 2 1202
挽巷
挽巷 2021-01-26 19:10

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

Right now I have the script and it runs perfectly in terminal but I was hoping to be able to create a double

相关标签:
2条回答
  • 2021-01-26 20:10

    Well you can write a quick shell script that's basically running your ruby script and make this shell script executable. See the link bellow:

    How to run a shell script in OS X by double-clicking?

    The content of your shell script can be as simple as that:

    ruby /yourpath/yourscript.rb
    
    0 讨论(0)
  • 2021-01-26 20:16

    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
    

    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.

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

    chmod a+x my_script.rb 
    

    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.

    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
    
    0 讨论(0)
提交回复
热议问题