Ruby: How to load a file into interactive ruby console (IRB)?
Asked Answered
N

8

111

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

Needlework answered 28/10, 2012 at 19:30 Comment(2)
Is [this][1] what you want to achieve ? [1]: https://mcmap.net/q/196176/-irb-how-to-start-an-interactive-ruby-session-with-pre-loaded-classesNagey
source("filename.rb")Retouch
U
104

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

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

Urnfield answered 9/1, 2013 at 17:41 Comment(4)
If you want to add more than just -r between each file, well that's what I do and it works. It's how I load all my files into irb so I can access all my app's stuff within irb, of course I use a rake script to load the boot file into irb so I can just rake irb and it does it all for me.Intersect
this was my first attempt after reading man, but i use -r some_file.rb, 30 minutes later i found this and add ./, thanks xdBobbybobbye
In Ubuntu 16.04, I remove the dot: irb -r /foo/foo.rbBille
This doesn't give access to variables from the file.Rivalee
S
58

Using ruby 1.9.3 on Ubuntu 14.04, I am able to load files from the current directory into irb with the following command line:

irb -I . -r foo.rb

where foo.rb is the file I want to load from my current directory. The -I option is necessary to add the current directory (.) to ruby's load path, as explained in the ruby man page. This makes it possible to require files from the current directory, which is what the -r option to irb accomplishes.

The key piece that wasn't obvious for me when I had this problem is the -I option. Once you do that, you can call require 'foo.rb' from within irb for any files in the current directory. And of course, you can specify any directory you want, not just . with the -I option. To include multiple directories on the load path, separate them with a colon (:), e.g.:

irb -I foo/:bar/:baz/

This command will add the directories foo, bar, and baz to ruby's load path.

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

irb -r ./foo.rb

or from within irb:

> require './foo.rb'
Sweepback answered 9/1, 2015 at 22:46 Comment(1)
The result of your solution is the same as calling 'ruby foo.rb', but at least you see what is going on. But I want to leave the console open, so I can execute some more commands. Any idea?Benghazi
M
39

Type in irb

And then

require './ruby_file.rb'

This is assuming that ruby_file.rb is in the same directory. Adjust accordingly.

Mckinney answered 16/12, 2014 at 16:13 Comment(4)
You can also use require_relative. E.g. require "./lib/foo" does the same as require_relative "lib/foo"Elsaelsbeth
.rb is optional, you can write require './ruby_file'. I checked in ruby 1.9.3p551`Maltase
what does the ./ do?Brocade
@Brocade it's a relative path to the current directoryActivate
R
33

Two ways:

  1. to load source without running the program -- this gives access to all variables and functions:

source("filename.rb")

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

require("filename.rb")

Retouch answered 22/7, 2016 at 18:42 Comment(1)
This appears to answer the question, how to reload a script as well. I had been using require which doesn't appear to reload the script (any modifications to original source are not reflected in your session), while source appears to reflect changes to original script.Imprimis
C
4

It depends on your ruby. Ruby 1.8 includes your current path, while ruby 1.9 does not. Evaluate $: to determine if your path is included or not. So in ruby 1.9 you must use the entire path, which is always a safe bet.

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

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

Check out Adding a directory to $LOAD_PATH (Ruby) if you are going to be using ruby 1.8

Cephalad answered 28/10, 2012 at 19:37 Comment(0)
C
2

Type the ruby codes in the text editor

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

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

After that,type irb and your filename(don't forget to include your extension(.rb)).

click here to see loading a ruby file using irb

In that image,I loaded a simple ruby file which only prints "ruby".

Caporal answered 30/6, 2017 at 10:13 Comment(1)
Good answer, but I suggest posting the terminal session as text rather than an image.Gragg
S
1

For those, who want to load .rb file from the different directory. Just add a string representer of the directory to $: variable.

> $: << "/directory/to/the/required/rb/file"
> require "file"
Simplehearted answered 20/8, 2019 at 11:30 Comment(0)
M
0

Another way to load the path into irb is just type require then drag and drop the file into the terminal.🙂 -tested using Linux Mint.

Marlysmarmaduke answered 1/3, 2017 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.