My irbrc
Introduction
Every single day (I open my computer) I use a Ruby REPL.
And since irb
was gemified it is moving forward fast and getting better and better.
I am trying to invest some time to get used to it and make it more useful for me.
Out of the box it provides a great experience. At some point I might write about the things I use or appreciate the most.
For example, the Don’t echo an expression’s result when it ends with a semicolon that is extremly useful when querying something that might return thousands of records (ie: User.where(stauts: :active)
).
The file
begin
require "rubygems"
gems = %w[
amazing_print
]
if defined?(Rails)
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gems.each do |gem_name|
gem gem_name
end
end
else
gems.each do |gem_name|
next if Gem::Specification.find_all_by_name(gem_name).any?
system("gem install #{gem_name}")
end
end
require "amazing_print"
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:AUTO_INDENT] = true
AmazingPrint.defaults = {
indent: 2,
sort_keys: true,
}
AmazingPrint.irb!
rescue LoadError => e
puts "Error loading console: #{e}"
end
What it does
As I mentioned, I like irb
as it is.
But I like it even more with amazing_print
.
So, on initialization it checks whether irb
is running inside a Rails application or not.
If it is, it uses bundler/inline
to install the gem. This way I do not have to add it to the Gemfile
of projects where I work with other people.
If it is not running inside a Rails application, it installs the gem using gem install
.
After that, it requires amazing_print
, sets some defaults for it and initializes it.
Pretty simple, but it makes my day a little bit better.