How to run ruby ​​files?

Example. I have a test.rb file:

puts "test test test" 

How to run this file in ruby ​​console?

+1
source share
4 answers

load 'test.rb'

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

+6
source
 load("test.rb") 

should do the trick in irb .

+6
source

On a Mac, you can run three different methods from the terminal. Method 1 In the irb (Interactive Ruby Shell) terminal to execute line by line, then close the command to exit irb.

Method 2 Since ruby ​​is an interpreted language, we can run one command on the terminal

  • Save the text editor code with the extension .rb.
  • Change the directory in the terminal with the cd command (cd drag the folder to the terminal so that you can be redirected to the directory).
  • ruby hello.rb

Method 3 ruby ​​-v know the ruby ​​version ruby ​​-e 'puts WVU' #line on a line in the terminal

+1
source

Any ruby ​​file can only be launched with ruby <your_ruby_file.rb> if you are in the same directory as the ruby ​​file; If not, just specify the file path:

 ruby path/to/your_file.rb 
0
source

Source: https://habr.com/ru/post/927873/


All Articles