Ruby: How to load a file into interactive ruby console (IRB)? - ruby

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?

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.

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'

Type in irb
And then
require './ruby_file.rb'
This is assuming that ruby_file.rb is in the same directory. Adjust accordingly.

Two ways:
to load source without running the program -- this gives access to all variables and functions:
source("filename.rb")
to run program and then drop into interactive mode -- this only gives access to functions, not variables:
require("filename.rb")

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

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)).
In that image,I loaded a simple ruby file which only prints "ruby".

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.

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"

Related

Get command line current folder in ruby gem

I'm writing a small ruby gem which should generate couple of files, so I'm using executables.
How can I (in my ruby code) get the current folder of the command line from which the gem was executed?
For example, when I run my tool in this path:
~/Projects/MyProject/ $ my-tool
How can I get the ~/Projects/MyProject/ in my ruby code?
Thank you
Dir.pwd or Dir.getwd should do. Please note they return full absolute path, with ~ expanded.
https://ruby-doc.org/core-2.5.0/Dir.html#method-c-pwd

Moving ruby and rubygems into a custom path

I have a local ruby interpreter created a third party which is installed under /usr/lib/projectA/ruby/bin/ruby
Now, I want to copy the whole folder strcuture into another folder with the same structure: /usr/lib/projectB/ruby/bin/ruby
After I copied the files, and call the copied ruby, e.g.
# /usr/lib/projectB/ruby/bin/ruby -v
ruby 1.9.x
Seems to be working, however, when I run
# /usr/lib/projectB/ruby/bin/ruby -e 'puts 1'
<internal:gem_prelude>:1:in `require': cannot load such file -- rubygems.rb (LoadError)
from <internal:gem_prelude>:1:in `<compiled>'
Seems it can't find the rubygems, so I add the path
# /usr/lib/projectB/ruby/bin/ruby -e 'puts 1' -I '/usr/lib/projectB/ruby/lib/'
/usr/lib/projectB/ruby/lib/ruby/1.9.1/rubygems.rb:31:in `require': cannot load such file -- rbconfig (LoadError)
Now, another files cannot be loaded, so I assume more to come..
So
What is the correct method to set the new rubygems base path for my new ruby?
Why even calling puts 1 will invoke the rubygems?
p.s. I can't use rvm or similar approach as we need to deploy the whole zip package with the ruby to our user.
You can use RVM and set the path in your .rvmrc file via rvm_path parameter.
Also double check your ~/.bashrc or ~/.bash_profile if the paths are OK.
Other option is to play with $PATH and $LD_LIBRARY_PATH for ruby and use $GEM_PATH which provides the locations (there may be several) where gems can be found.
You probably will need to play with Ruby's environmental variables:
RUBYOPT Additional command-line options to Ruby; examined after real command-line options are parsed ($SAFE must be 0).
RUBYLIB Additional search path for Ruby programs ($SAFE must be 0).
RUBYPATH With -S option, search path for Ruby programs (defaults to PATH).
RUBYSHELL Shell to use when spawning a process; if not set, will also check SHELL or COMSPEC.
DLN_LIBRARY_PATH Search path for dynamically loaded modules.
RUBYLIB_PREFIX (Windows only) Mangle the RUBYLIB search path by adding this prefix to each component.
from here.

Why does irb -r make __FILE__ an absolute path?

foo.rb is a one-liner that puts __FILE__
irb -r ./foo.rb
gives me the absolute path to foo.rb. This is not the behavior if I run ruby foo.rb. Why is it happening in irb?
irb 0.9.6(09/06/30),
ruby 1.9.3p327
The reason this happens in IRB is the same reason this would happen if you had written a program named bar.rb with the following contents.
require './foo'
You will find that using IRB with the require as you do is no different than calling bar.rb which has the require.
From the documentation:
__FILE__ -- The name of the file currently being executed, including
path relative to the directory where the application was started up
(or the current directory, if it has been changed).
So this is including the path relative to the directory where the application was started up. Who knows where irb or bar are? When it is not clear, then the path is given as well.

ruby require relative files

I'm using ruby v1.9.1 in combination with vim and I execute my scripts with:
:!ruby "%"
my scripts are running fine if I add:
$:.unshift File.dirname(__FILE__)
to add the path of this file to the LOAD_PATH of ruby. If I omit this line my require statements to local scripts aren't working anymore.
Is there a way to pass the path of the file to rubys LOAD_PATH? Something like (completly fictional):
:!ruby "%" --add-to-load-path
I did some research before and stubled upon require_relative, but this has the same effect as require and is not working.
You can use the -I option of the ruby executable and write something like the following:
:!ruby -I%:p:h. %
See ruby --help for further information and file modifiers.
Edited: see comments.

Include files in a command line with Ruby

When running ruby scripts as such
ruby some-script.rb
How would I include a file (e.g. configuration file) in it dynamically?
As you have found, the -r option is your friend. It also works with IRB:
irb -ropen-uri
Will do the same as require 'open-uri'
FWIW, the most common thing I need to include via the command line is rubygems. And since newer versions of ruby come with gems built in I don't want to edit the file, but include it for testing. Luckily the folks who created gems added a little alias sugar.
You can do the following:
ruby -rubygems myscript.rb
Instead of the ugly:
ruby -rrubygems myscript.rb
OK, so it is one character, but thought it was extra polish to make me happier.
Actually, I found it. It's the -r command line entry.
-r <library_name>
This causes Ruby to load the library using require.
It is useful when used in conjunction with -n or -p.
You can use:
require 'some_ruby_file'
in some-script.rb. It will load some_ruby_file.rb.
Before you call require "somefile.rb" you must navigate to the folder that the file is located or you must provide the full path. In example: require "~/Documents/Somefolder/somefile.rb"

Resources