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

不羁的心 提交于 2019-11-30 10:16:54

问题


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?


回答1:


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.




回答2:


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'



回答3:


Type in irb

And then

require './ruby_file.rb'

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




回答4:


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")




回答5:


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




回答6:


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".




回答7:


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.




回答8:


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 "some_file"


来源:https://stackoverflow.com/questions/13112245/ruby-how-to-load-a-file-into-interactive-ruby-console-irb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!