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

后端 未结 2 1231
挽巷
挽巷 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: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
    

提交回复
热议问题