¶ ↑
irbtoolsirbtools improves Ruby’s irb console. Unlike pry, you are still in your normal irb, but you have colors and lots of helpful methods.
¶ ↑
Setupgem install irbtools
-
On Linux, you need
xclip
orxsel
to use the clipboard: sudoapt-get install xclip
-
On Windows, you need ansicon to enable colors
-
On MacOS, you need growl, to use the
g
gem.
¶ ↑
UsageTo use irbtools, put the following in your ~/.irbrc
file (it is loaded every time you start irb):
require 'irbtools'
If the file does not exist, just create a new one. See further below on how to customize loaded libraries.
¶ ↑
Rails notesTo integrate irbtools into a rails console, you can either add irbtools
to your Gemfile:
gem 'irbtools', :require => false
Another way is to add a debundle hack on top of your ~/.irbrc
:
# modified from https://github.com/ConradIrwin/pry-debundle/pull/5 if defined?(Gem.post_reset_hooks) Gem.post_reset_hooks.reject!{ |hook| hook.source_location.first =~ %r{/bundler/} } Gem::Specification.reset load 'rubygems/custom_require.rb' alias gem require end
¶ ↑
Yesterday’s Ruby versionsirbtools focuses on Ruby 1.9 support. To get it working with 1.8.7, use the following line in your Gemfile:
gem 'irbtools', :git => "git://github.com/janlelis/irbtools.git", :branch => 'legacy', :require => false
¶ ↑
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 into the irbtools-more gem. To use them, you need to change your .irbrc
to:
require 'irbtools/configure' Irbtools.add_package :more Irbtools.start
¶ ↑
Included gems and librariesSee 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.
- wirb
-
Colorizes resulting Ruby objects
- paint
-
Provides easily to use terminal colors
- hirb
-
Custom views for specific objects, e.g. tables for ActiveRecord
- fancy_irb
-
Hash rockets for results and colorful error messages
- every_day_irb
-
Contains helper methods that might be useful in every-day irb usage, see below for details
- clipboard
-
Easy clipboard access
- interactive_editor
-
Lets you open vim (or emacs) from within irb to hack something that gets loaded into the current session, also possible: yaml object editing
- zucker
-
Nice debug printing (
q
,o
,c
,Object#m
,Object#d
) and useful pseudo-constants (Info
,OS
,RubyVersion
,RubyEngine
) - ap
-
Alternative for displaying Ruby objects
- coderay
-
Colorizes Ruby code (
colorize
,ray
) - boson
-
“A command/task framework similar to rake and thor that opens your ruby universe to the commandline and irb.”
- methodfinder
-
Interesting method finder (
mf
) - ori
-
Adds an
Object#ri
method - method_locator
-
Provides
Object#mlp
(improved version ofModule#ancestors
) andObject#methods_for(m)
(get this method from all ancestors) - method_source
-
Object#src
can be shown for Ruby methods - bond
-
irbtools-more: Improves irb tab-completion
- looksee
-
irbtools-more: Great load path inspector:
Object#l
(Extended version ofObject#m
), also provides the ability toObject#edit
methods. - drx
-
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- sketches
-
Another, more flexible “start editor and it gets loaded into your irb session” plugin. It’s a great gem, but the reason it’s not included anymore is that it’s functionality is very similar to the one provided by the interactive_editor gem
- guessmethod
-
Automatically tries to corrects typos in method names
- rvm_loader
-
RVM helpers
¶ ↑
Irbtools methodsThe following general helper methods are defined by every_day_irb (which belongs to irbtools)
- ls
-
Returns an array with the directory’s content
- cat
-
Shortcut for
File.read
- rq
-
Shortcut for
require library.to_s
(allows concise syntax likerq:mathn
) - ld
-
Shortcut for
load library.to_s + '.rb'
- rrq/rerequire
-
Little hack for rerequiring a library (it’s really hack and not reliable, but works in most cases)
- reset!
-
Restarts irb
- clear
-
Clears your irb terminal (
system "clear"
) - session_history
-
Returns all issued commands as string
Irbtools also defines some more helpers in combination with the loaded gems:
- cd
-
Improves the cd that is already provided by FileUtils (try
cd '-'
) - rv
-
Displays RubyVersion (zucker)
- re
-
Displays RubyEngine (zucker)
- colorize
-
Syntax highlights a ruby string using CodeRay
- ray
-
Syntax highlights a ruby file using CodeRay
- copy
-
Shortcut for
Clipboard.copy
- paste
-
Shortcut for
Clipboard.paste
- copy_input
-
Copies the session_history to the clipboard
- copy_output
-
Copies this session’s results to the clipboard
- mf
-
Shortcut for using the methodfinder
- page
-
Shortcut for using the pager from hirb
These are the custom public Object
methods renamed/patched for some gems
- ri
-
Patching the
ri
provided by ori to also allow default ri syntax on toplevel - src
-
Shortcut for displaying the method source using method_source and coderay
- l/ll
-
Alternative method name to trigger the looksee gem (irbtools-more)
¶ ↑
Advanced tweaking¶ ↑
How to load more or less librariesIt’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)
-
Irbtools.remove_library(lib)
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 causes 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-upYou 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
¶ ↑
Welcome messageThe welcome message can be customized with Irbtools.welcome_message=
¶ ↑
PackagesThere are irbtools extension packages, which can be modified via:
-
Irbtools.add_package(pkg)
-
Irbtools.remove_package(pkg)
These packages add/modify the libraries to be loaded. For an example, see irbtools-more.
¶ ↑
CopyrightCopyright © 2010-2012 Jan Lelis, rbjl.net, released under the MIT license.