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.
Upgrading irb on OS X
with rvm you can turn on completion:
https://rvm.io/workflow/completion/
this loads the scripts in your ~/.rvm/scripts
dir including irbrc.rb
which sets up autocompletion for irb.
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
How do I get the irb(main):001:0 prompt instead of
$ irb --help
Usage: irb.rb [options] [programfile] [arguments]
--prompt prompt-mode
--prompt-mode prompt-mode
Switch prompt mode. Pre-defined prompt modes are
`default', `simple', `xmp' and `inf-ruby'
$ irb --prompt inf-ruby
irb(main):001:0>
How can I clear the rails console history
I interpret you question as asking how to turn history on in the Rails Console and off in the Ruby debugger. If this isn't true, please clarify.
IRB, and by extension, the Rails Console, read from ~/.irbrc
, or if that doesn't exist, /etc/irbrc
, to startup and configure irb. Your history is typically written to ~/.irb_history
, but that is dictated by the contents of your irbrc
file. The /etc/irbrc
on my Mac OS X is set up to write the history from irb, so perhaps you've created a local .irbrc
that doesn't have history, or perhaps you have a syntax error in that file.
The debugger reads a file called .rdebugrc
on startup. You can turn off history in debug by adding this line to ~/.rdebugrc
:
set history save off
Turn it back on with:
set history save on
You could also set your debug output to go to a different file than irb reads from with the command:
set history filename
These also work from the debug prompt, but aren't persistent.
How to clear the historial of irb using RVM
I've looked through my irbrc.rb
and compared it to yours, and I noticed that everything is essentially the same, except for your lines 41 to 49. Perhaps it was manually edited by someone? Whatever the case, if you replace these lines:
rvm_ruby_string = ENV["rvm_ruby_string"] ||
(
ENV['GEM_HOME'] &&
path = ( File.realpath(ENV['GEM_HOME'].to_s) rescue nil ) &&
( path = $1 if path =~ /(.+)\/$/ ; true ) &&
path.split(/\//).last.split(/@/).first
) ||
("#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}" rescue nil) ||
(RUBY_DESCRIPTION.split(" ")[1].sub('p', '-p') rescue nil ) ||
(`ruby -v` || '').split(" ")[1].sub('p', '-p')
with these lines:
rvm_ruby_string = ENV["rvm_ruby_string"] ||
ENV['GEM_HOME'].nil? ? `ruby -v`.match(/^([^ ]+ [^ ]+)/)[0].sub(/ /,'-') : ENV['GEM_HOME'].split(/\//).last.split(/@/).first
then your irbrc.rb
file will match mine. Another you could probably do is update your ruby version, using rvm get stable
.
Related Topics
Getting a Rogue Iteration from My .Each Loop
Instance_Eval Does Not Work with Do/End Block, Only with {}-Blocks
Can't Dup Nilclass on Association Methods
Bootstrap Modal in Ruby on Rails Not Working
Ruby Parenthesis Syntax Exception with I++ ++I
Using Poltergeist with a Proxy
How to 'Join' an Array Adding to the Beginning of the Resulting String the First Character to Join
Ruby Facebook Graph API Appsecret_Proof
How to Understand the #Dup and #Clone Operate on Objects Which Referencing Other Objects
Rails 404 Error for Stylesheet or JavaScript Files
Calling Instance Variables Without @
Stubbing Controller Actions in Rspec Request Specs
How to Sort So That "Vitamin B12" Is Not in Front of "Vitamin B6"
Undefined Method Error When Creating Delayed_Job Workers with Script/Delay_Job
Import CSV in Batches of Lines in Rails
How to Mix Required Argument and Optional Arguments in Ruby