The irbtools gem is a meta gem that installs handy and useful irb gems and provides an appealing ready-to-use irb configuration. It has a modular structure and supports multiple library-loading mechanisms, e.g. via autoload or threads.
gem install irbtools
On Linux, you need xclip or xsel to use the clipboard: sudo apt-get install xclip
On Windows, you need ansicon to enjoy all the colors
On MacOS, you need growl, to use the g gem.
To use irbtools, put the following in your ~/.irbrc file (this file is loaded every time you start irb):
require 'rubygems' unless defined? Gem # rubygems is only needed in 1.8 require 'irbtools'
If the file does not exist, just create a new one. See further below on how to customize loaded libraries.
To integrate irbtools into a rails console, you will have to add irbtools to your Gemfile in this way: (or there will be some bundler double require issues..)
gem 'irbtools', :require => false
You can put Rails specific configuration in a ~/.railsrc file that only gets loaded in Rails environments.
See this blog post for some Rails goodies.
This is irbtools “light”
When installing irbtools, some gems won't be installed to keep windows support, e.g. better auto-completion or the looksee gem. These are packaged in the irbtools-more gem. To use them, you need to change your .irbrc to:
require 'irbtools/configure' Irbtools.add_package :more Irbtools.start
In that case, the Gemfile line looks like this:
gem 'irbtools-more', :require => false
Included gems and libraries
See rbjl.net/40-irbtools-release-the-power-of-irb or read the commented source files to get some examples of what you can do with irbtools.
Colorizes Ruby objects (used for the results)
Provides easily to use terminal colors
Custom views for specific objects, e.g. tables for ActiveRecord
Hash rockets for results and colorful error messages
Contains helper methods that might be useful in every-day irb usage, see below for details
Easy clipboard access
Lets you open vim from within irb to hack something that gets loaded into the current session, also possible: yaml object editing
Another, more flexible “start editor and it gets loaded into your irb session” plugin
Nice debug printing (q, o, c, Object#m, Object#d) and useful pseudo-constants (Info, OS, RubyVersion, RubyEngine)
Nice debug printing commands (ap) [also possible: wp from wirb/wp]
Colorizes Ruby code (colorize, ray)
“A command/task framework similar to rake and thor that opens your ruby universe to the commandline and irb.”
Interesting method finder (mf)
Adds an Object#ri method
Provides Object#method_lookup_path (improved version of Module#ancestors) and Object#methods_for (get this method from all ancestors)
Object#src can be shown for Ruby methods
irbtools-more: Improves irb tab-completion
irbtools-more: Great load path inspector: Object#l (Extended version of Object#m), also provides the ability to Object#edit methods.
irbtools-more: A tk object inspector, defines Object#see
- fileutils (stdlib)
Includes file system utility methods: cd, pwd, ln_s, mv, rm, mkdir, touch, … ;)
Not included anymore
Automatically tries to corrects typos in method names
The following general helper methods are defined by every_day_irb (which belongs to irbtools)
Returns an array with the directory's content
Shortcut for File.read
Shortcut for require library.to_s (allows concise syntax like rq:mathn)
Shortcut for load library.to_s + '.rb'
Little hack for rerequiring a library (it's really hack and not reliable, but works in most cases)
Clears your irb terminal (system "clear")
Returns all issued commands as string
Shortcut for debugger that also requires 'ruby-debug' if needed
Irbtools also defines some more helpers in combination with the loaded gems:
Improves the cd that is already provided by FileUtils (try cd '-')
Displays RubyVersion (zucker)
Displays RubyEngine (zucker)
Syntax highlights a ruby string using CodeRay
Syntax highlights a ruby file using CodeRay
Shortcut for Clipboard.copy
Shortcut for Clipboard.paste
Copies the session_history to the clipboard
Copies this session's results to the clipboard
Shortcut for using the methodfinder
These are the custom public Object methods renamed/patched for some gems
Patching the ri provided by ori to also allow default ri syntax on toplevel
Shortcut for displaying the method source using method_source and coderay
Alternative method name to trigger the looksee gem (irbtools-more)
How to load more or less libraries
It's possible to modify, which libraries should get loaded:
# don't require 'irbtools', but: require 'irbtools/configure' # here you can modify the libraries using the methods below Irbtools.start
If you don't want to load the default set of irbtools gems, you will have to use require 'irbtools/minimal' instead of configure.
You have the following methods:
Irbtools.add_library(lib, options_hash, &block)
The options_hash defines the way in which Irbtools loads the library. Following options are possible
- (no options)/:start
The library is required on startup before doing anything else (before displaying the prompt)
- :thread => identifier
After loading everything else, the library is required in a thread (while continuing loading). You can choose any identifier, but if you take the same one for multiple libraries, they will be loaded in the same thread (in the order that you define)
- :late => true
The library is required just before showing the prompt (note: loading threads might still be in process)
- :late_thread => identifier
Same as :thread, but after loading late libraries.
- :sub_session => true
The library is loaded every time a sub-session starts (using IRB.conf[:IRB_RC]). In ripl, ripl-after_rc is used.
- :autoload => :Constant
Use Ruby's autoload feature. It loads the library as soon as the constant is encountered.
You can also apply a block that gets executed after the library is loaded (except for autoload, the code will executed on startup in this case). You can also just add callback block by using the Irbtools.add_library_callback or the Irbtools.replace_library_callback method.
When adding a new library, you should firstly consider some way to load it via :autoload. If not possible, try loading via thread. If that is not possible either, fallback to the default loading.
Troubleshooting: Unicode caused Wrong display widths?
When using double-width unicode chars, you need to paste the following snippet to your .irbrc.
Irbtools.replace_library_callback :fancy_irb do FancyIrb.start :east_asian_width => true end
This setting is deactivated by default because of performance issues.
Hint: Faster start-up
You can get an about a second faster start-up time by changing the loading methods for wirb and fancy_irb to :thread (drawback: the hash rocket will not be used for the first result):
require 'irbtools/configure' Irbtools.remove_library :paint Irbtools.remove_library :fancy_irb Irbtools.add_library :paint, :late => true do Wirb.load_schema :classic_paint if defined? Wirb end Irbtools.add_library :fancy_irb, :thread => -1 do FancyIrb.start end Irbtools.start
The welcome message can be customized with Irbtools.welcome_message=
There are irbtools extension packages, which can be modified via:
These packages add/modify the libraries to be loaded. For an example, see irbtools-more.
Copyright © 2010-2012 Jan Lelis, rbjl.net, released under the MIT license.