How to run ruby files?

放肆的年华 提交于 2020-01-21 20:22:02

问题


Example I have the file test.rb:

puts "test test test"

How do I run this file in the ruby console?


回答1:


load 'test.rb'

Do you mean Rails console? (Same thing, but the question is tagged rails.)




回答2:


load("test.rb")

should do the trick in irb.




回答3:


On Mac you can run in three different ways from the terminal Method 1 On Terminal irb (Interactive Ruby Shell) for line by line execution then quit command to be out of irb.

Method 2 As ruby is Interpreted language we can run by one command on terminal

  1. Save the text editor code with .rb extension.
  2. Change the directory in terminal by cd command (cd drag and drop the folder on terminal so that you can be directed to the directory).
  3. ruby hello.rb

Method 3 ruby -v to know the version of ruby ruby -e 'puts WVU' #line by line execution on terminal




回答4:


Any ruby file can be run with just ruby <your_ruby_file.rb>, supposing that you are in the same directory as the ruby file; If not, just provide a path to the file:

ruby path/to/your_file.rb


来源:https://stackoverflow.com/questions/7626807/how-to-run-ruby-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!