I'm switching irb to pry. I just found that file name completion in pry didn't work.
I have a foo.rb in current directory, then I can complete file name like irb> load 'f[TAB], but this completion doesn't work in pry.
Is there configuration for this, or it is not possible to use file name completion in pry?
You can use "shell-mode" or "file-mode (alias for shell-mode)" in pry.
Related
The latest version of irb introduced an autocomplete that is quite buggy and I don't generally like to be distracted by an autocomplete, any idea how I can disable it?
Related question: How to disable Pry autocomplete?
Try putting this in your ~/.irbrc
IRB.conf[:USE_AUTOCOMPLETE] = false
We can pass the --noautocomplete command line option when invoking IRB.
irb --noautocomplete
Alternatively we can set a configuration option in ~/.irbrc or one of the other locations specified in the documentation
IRB.conf[:USE_AUTOCOMPLETE] = false
Starting a Rails console will respect this configuration but for a one-off we can provide the command line option.
Be sure to pass any Rails options before the --
rails console --sandbox -- --noautocomplete
Linux: locate .irbrc file. If there is no .irbrc file - create one in your home folder - type the following command into terminal:
echo "IRB.conf[:CONTEXT_MODE] = 0" >> ~/.irbrc
Windows: locate .irbrc folder
C:\Users\Ben\ .irbrc
Then to disable autocomplete add this line to .irbrc file:
IRB.conf[:USE_AUTOCOMPLETE] = false
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.
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"
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.
Might sound like a newbie question (and it is since I am new to Ruby and IRB) but is there a way to save the commands you did in IRB to file? I am playing with WATIR and would love to save all my inputs to file instead of copying and pasting each.
Thanks for the help!
On my machine I can put this in my .irbrc file (located in your home directory):
Kernel.at_exit {
File.open("irb.log", "w") do |f|
f << Readline::HISTORY.to_a.join("\n")
end
}
It creates a file irb.log that contains your readline history. Irb uses readline for command input. It might be configured not to use readline for some people, I don't know. And maybe the history will be truncated at some point, or maybe it'll be modified by certain commands you do in your irb session... but try it out and see if it works.
If you want the irb prompt and the result of each command to be included in the log, then just use tee to record the output of irb:
$ irb | tee irb.log
You can run vim in irb:
http://vimcasts.org/episodes/running-vim-within-irb/
Take a look at watir-console.
I found this question when looking to do the same thing. I ended up switching from IRB to Pry; it is a separate REPL project for Ruby that has a whole host of advanced features not supported in IRB.
Well worth a look.
Pry