How to execute a Ruby script in Terminal?

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 program in Redcar and saved it as a .rb, but I don't know how to execute it through Terminal. I want to run the program and see if it actually works. How do I do this?


Just call: ruby your_program.rb

or

  • start your program with #!/usr/bin/env ruby ,
  • make your file executable by running chmod +x your_program.rb
  • and do ./your_program.rb some_param

  • 假设红宝石解释器在你的PATH中(它应该是),你只需运行

    ruby your_file.rb
    

    Open your terminal and open folder where file is saved.
    Ex /home/User1/program/test.rb

  • Open terminal
  • cd /home/User1/program
  • type: ruby test.rb
  • format or test.rb

    class Test 
      def initialize
       puts "I love India"
      end
    end
    
    # initialize object
    Test.new
    

    output

    I love India
    
    链接地址: http://www.djcxy.com/p/19178.html

    上一篇: 如何在Linux / Unix上永久设置$ PATH?

    下一篇: 如何在终端中执行Ruby脚本?