IRB history not working with Ruby 2.3.0
OS X's command-line editing is based on the libedit library. OS X has a version of the readline library which is a wrapper around libedit, but it does not behave completely like GNU readline. irb history works in Ruby built with OS X's wrapper up to Ruby 2.1, but Ruby 2.2 and later need to be built with GNU readline for irb history to work.
In the following, 2.3.0
can be any Ruby version from 2.2.0 on. I wrote 2.3.0
since that's what Evan used.
Using Homebrew
If you install ruby using homebrew, it will bring with a working version of readline.
brew install ruby
Then follow the instructions to add it to your PATH. Then execute gem install irb
if it says can't find gem irb
.
Using MacPorts
rbenv doesn't know about MacPorts, so you need to explicitly tell it to use MacPorts' readline.
sudo port install readline
if it isn't installed already.rbenv uninstall 2.3.0
RUBY_CONFIGURE_OPTS=--with-readline-dir=/opt/local rbenv install 2.3.0
Using Homebrew with rbenv
rbenv automatically detects homebrew and looks in it for readline, so, if you're using Homebrew and irb history doesn't work, you either haven't installed readline or you built your Ruby before you installed readline.
brew install readline
if it isn't installed alreadyrbenv uninstall 2.3.0
rbenv install 2.3.0
irb history not working
I don't have an answer for you why the above doesn't work, but I did find a file, /etc/irbrc
on my system (OS X - Snow Leopard, Ruby 1.8.7) that does provide a working, persistent history for me. So two pieces of advice: i) check your /etc/irbrc (or equivalent) to make sure that there isn't anything in there that might interfere with your settings, and ii) try out the settings below to see if you can get history working that way.
# Some default enhancements/settings for IRB, based on
# http://wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks
unless defined? ETC_IRBRC_LOADED
# Require RubyGems by default.
require 'rubygems'
# Activate auto-completion.
require 'irb/completion'
# Use the simple prompt if possible.
IRB.conf[:PROMPT_MODE] = :SIMPLE if IRB.conf[:PROMPT_MODE] == :DEFAULT
# Setup permanent history.
HISTFILE = "~/.irb_history"
MAXHISTSIZE = 100
begin
histfile = File::expand_path(HISTFILE)
if File::exists?(histfile)
lines = IO::readlines(histfile).collect { |line| line.chomp }
puts "Read #{lines.nitems} saved history commands from '#{histfile}'." if $VERBOSE
Readline::HISTORY.push(*lines)
else
puts "History file '#{histfile}' was empty or non-existant." if $VERBOSE
end
Kernel::at_exit do
lines = Readline::HISTORY.to_a.reverse.uniq.reverse
lines = lines[-MAXHISTSIZE, MAXHISTSIZE] if lines.nitems > MAXHISTSIZE
puts "Saving #{lines.length} history lines to '#{histfile}'." if $VERBOSE
File::open(histfile, File::WRONLY|File::CREAT|File::TRUNC) { |io| io.puts lines.join("\n") }
end
rescue => e
puts "Error when configuring permanent history: #{e}" if $VERBOSE
end
ETC_IRBRC_LOADED=true
end
rbenv irb history is not saving
I found this way for solving my problem. In file ~/.irbrc write:
require 'irb/ext/save-history'
#History configuration
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
Found in this question: irb history not working
Is there an irb return history?
Try conf.eval_history
. "irb idea: return value history" is an example of its use and is probably the closest thing I am aware of off the shelf.
Is /etc/irbrc installed by OS X? Does irb read it?
Per Mark Setchell's and Jared Beck's comments, /etc/irbrc is installed with current (10.13) OS X and has been for at least a few major releases.
I copied /etc/irbrc to ~/.irbrc, ran irb, exited and got an error: undefined method 'nitems' for ["exit"]:Array (NoMethodError)
. This method existed in Ruby 1.8 but was removed from Ruby 1.9. I don't normally see this error, so I conclude that /etc/irbrc isn't executed at all.
Overall, I conclude that I can ignore /etc/irbrc when debugging issues with my ~/.irbrc, which was the origin of my question.
Related Topics
How to Generate a Human Readable Time Range Using Ruby on Rails
How to Read the Content of an Excel Spreadsheet Using Ruby
Rails Route to Username Instead of Id
Errno::Econnrefused: Connection Refused - Connect(2) for Action Mailer
Get Index of String Scan Results in Ruby
Rails: Should Partials Be Aware of Instance Variables
Ruby Convert Array to Nested Hash
How to Randomly Sort (Scramble) an Array in Ruby
Combination of Two Arrays in Ruby
Differencebetween 'After_Create' and 'After_Save' and When to Use Which
Ruby: How to Convert a String to Boolean
Creating a Model That Has a Tree Structure
How to Automate Chrome Request Blocking Using Selenium-Webdriver for Ruby
Problems Installing Ruby on Mountain Lion - Ruby 1.9.3 Wont' Compile
Ruby on Rails Plural (Controller) and Singular (Model) Convention - Explanation
How to Set Up Simple Http Authentication for a Ruby on Rails App on Heroku