Ruby: How to upload a file to an interactive ruby ​​console (IRB)?

I am using IRB (ruby interactive console) to learn how to program using Ruby. How to upload a file to the console if I write my programs in a text editor first?

+77
ruby irb
Oct 28 '12 at 19:30
source share
8 answers

If you need to load only one file in IRB, you can call it with irb -r ./your_file.rb if it is in the same directory.

This automatically requires a file and allows you to work with it immediately.

+76
Jan 09 '13 at 17:41
source share

Using ruby ​​1.9.3 on Ubuntu 14.04, I can upload files from the current directory to irb using the following command line:

 irb -I . -r foo.rb 

where foo.rb is the file that I want to download from the current directory. The -I option is needed to add the current directory ( . ) To the ruby ​​boot path, as described on the ruby ​​man page. This allows require files from the current directory, which is what the -r option to irb .

The key thing that was not obvious to me when I had this problem was the -I option. After that, you can call require 'foo.rb' from irb for any files in the current directory. And, of course, you can specify any directory that you want, and not just . with the option -I . To include multiple directories in the download path, separate them with a colon (:), for example:

 irb -I foo/:bar/:baz/ 

This command will add the foo , bar and baz directories to the ruby ​​boot path.

The ultimate alternative is to use a relative or absolute path to the file when using require or -r to load the file:

 irb -r ./foo.rb 

or from irb :

 > require './foo.rb' 
+39
Jan 09 '15 at 22:46
source share

Enter irb

And then

 require './ruby_file.rb' 

It is assumed that ruby_file.rb is in the same directory. Set accordingly.

+31
Dec 16 '14 at 16:13
source share

Two ways:

  • to load the source without starting the program - this gives access to all variables and functions:

source("filename.rb")

  1. run the program, and then go into interactive mode - this gives access only to functions, and not to variables:

require("filename.rb")

+20
Jul 22 '16 at 18:42
source share

It depends on your ruby. Ruby 1.8 includes your current path, but ruby ​​1.9 does not. Rate $: to determine if your path is included or not. So in ruby ​​1.9 you have to use the whole path, which is always a safe bet.

Then you can use require or load to include the file.

require does not require adding a file suffix when trying to find it and will only include one file. require should be used instead of load most of the time.

Note Adding a directory to $ LOAD_PATH (Ruby) if you intend to use ruby ​​1.8

+3
Oct 28 '12 at 19:37
source share

Enter ruby ​​codes in a text editor

Save it with the extension .rb (for example: demo.rb ).

In linux, open your terminal, then change the directory to the current location of this file (the cd command is used to change the directory).

After that, enter irb and your file name (do not forget to specify the extension (.rb)).

click here to view ruby ​​file upload using irb

In this image, I uploaded a simple ruby ​​file that prints only "ruby".

+2
Jun 30 '17 at 10:13
source share

Another way to load the path into irb is to simply type in the type and then drag the file to the terminal. -programmed using Linux Mint.

0
Mar 01 '17 at 17:11
source share

For those who want to download the .rb file from another directory. Just add the string representation of the directory to the $: variable.

 > $: << "/directory/to/the/required/rb/file" > require "some_file" 
0
Aug 20 '19 at 11:30
source share



All Articles