Load file to current directory when running irb

Asked

Viewed 1,446 times

9

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

irb -r date

But this does not work if I want to load a file in the directory where the command runs:

irb -r meuscript

(assuming the file exists meuscript.rb)

I think this worked in older versions. How it works now?

  • Which version of Ruby you’re using?

  • I’m using Ruby 1.9.3 or 2.0.

4 answers

13


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

irb -r ./meuscript

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

6

You can pass the file name. For example:

irb meuscript.rb

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

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

$ irb
1.9.3-p194 :001 > load 'meuscript.rb'
  • It does not work in the same way as in the example of irb -r date. In your example the irb closes after the file is loaded instead continue in interactive mode.

2

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

For example, suppose you are in a directory with a file my_app.rb. A way to start the irb with that uploaded 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, working on projects that do not obey the organization of Gems directories, etc.

  • This is especially efficient when there is more than one file. For example, Gem fonts are inside the directory lib by convention. When implementing irb -I ./lib, you get access to Gem as if it were installed.

0

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

require File.expand_path '<nome_do_arquivo>'

Detail, don’t put . Rb, only .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.