简体   繁体   English

如何运行ruby文件?

[英]How to run ruby files?

Example I have the file test.rb: 示例我有文件test.rb:

puts "test test test"

How do I run this file in the ruby console? 如何在ruby控制台中运行此文件?

load("test.rb")

应该在irb

load 'test.rb'

Do you mean Rails console? 您是说Rails控制台吗? (Same thing, but the question is tagged rails.) (同样,但问题被标记为rails。)

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. 在Mac上,您可以从终端运行三种不同的方法。方法1在终端irb(交互式Ruby Shell)上逐行执行,然后退出irb退出命令。

Method 2 As ruby is Interpreted language we can run by one command on terminal 方法2:由于ruby是解释性语言,因此我们可以在终端上通过一个命令来运行

  1. Save the text editor code with .rb extension. 保存扩展名为.rb的文本编辑器代码。
  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). 通过cd命令更改终端中的目录(将cd拖放到终端上,以便可以定向到该目录)。
  3. ruby hello.rb 红宝石hello.rb

Method 3 ruby -v to know the version of ruby ruby -e 'puts WVU' #line by line execution on terminal 方法3 ruby​​ -v知道ruby ruby​​ -e的版本'在终端上逐行执行'puts WVU'#

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; 假设您与ruby文件位于同一目录,则可以仅使用ruby <your_ruby_file.rb>运行任何ruby文件。 If not, just provide a path to the file: 如果没有,请提供文件的路径:

ruby path/to/your_file.rb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM