Upload file to the current directory when running irb

9

It is possible to call the irb by passing as a parameter a library to be loaded (required):

irb -r date

But this does not work if I want to load a file into the directory where the command is executed:

irb -r meuscript

(assuming the meuscript.rb file exists)

I think this worked on older versions. How does it work now?

    
asked by anonymous 11.12.2013 / 18:20

4 answers

13

For Ruby versions starting at 1.9.x, it is necessary to pass the complete path of the file, either relative or absolute, because the current directory has been removed from LOAD_PATH , so it is necessary to do:

irb -r ./meuscript

indicating that the meuscript.rb file is in the current directory.

    
11.12.2013 / 20:55
7

You can pass the file name. For example:

irb meuscript.rb

As pointed out by the rodrigorgs irb will exit after running the file.

The solution I see for this at the moment is to use load within irb:

$ irb
1.9.3-p194 :001 > load 'meuscript.rb'
    
11.12.2013 / 18:33
3

The path where irb will look for your libraries is decided by some environment variables (eg GEM_PATH , GEM_HOME ), which (usually) does not include the current directory. One way to change the path used in this search is the -I flag.

For example, suppose you are in a directory with a my_app.rb file. One way to start irb with this loaded file is:

$ irb -I. -r my_app
# Você também pode fazer:
$ irb -I.
>> require 'my_app'

This is very useful when testing loose scripts, moving projects that do not obey the organization of gems directories, etc.

    
29.01.2014 / 16:27
0

When I'm in IRB and need to use a lib of my own, I go into it and write:

require File.expand_path '<nome_do_arquivo>'

Detail, do not put .rb, only.

    
29.01.2014 / 16:45