How to execute a Ruby script in Terminal?

前端 未结 9 1587
南笙
南笙 2020-12-04 06:15

I\'ve set everything up that I need on my Mac (Ruby, Rails, Homebrew, Git, etc), and I\'ve even written a small program. Now, how do I execute it in Terminal? I wrote the pr

相关标签:
9条回答
  • 2020-12-04 07:13

    To call ruby file use : ruby your_program.rb

    To execute your ruby file as script:

    1. start your program with #!/usr/bin/env ruby

    2. run that script using ./your_program.rb param

    3. If you are not able to execute this script check permissions for file.
    0 讨论(0)
  • 2020-12-04 07:17

    Open Terminal

    cd to/the/program/location
    ruby program.rb
    

    or add #!/usr/bin/env ruby in the first of your program (script tell that this is executed using Ruby Interpreter)

    Open Terminal

    cd to/the/program/location
    chmod 777 program.rb
    ./program.rb
    
    0 讨论(0)
  • 2020-12-04 07:18

    For those not getting a solution for older answers, i simply put my file name as the very first line in my code.

    like so

     #ruby_file_name_here.rb
    
     puts "hello world"
    
    0 讨论(0)
提交回复
热议问题