/irbtools

Collection of IRB improvements

Primary LanguageRubyMIT LicenseMIT

irbtools

Improvements fro Ruby’s IRB console. Everything is colorful and you will get lots of console helpers. Unlike with PRY, you are still in your normal IRB. It is designed to work out-of-the-box so there is no reason to not use it!

Setup

$ gem install irbtools
  • On Linux, you will need xclip or xsel for a functional clipboard: sudo apt-get install xclip

  • On Windows, you will need ansicon to enable ANSI colors

  • On Mac OS, you will need growl, to use the g gem

Usage

To 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/Bundler Notes

To integrate irbtools into a Rails console, you should irbtools to your Gemfile:

gem 'irbtools', require: 'binding.repl'

Thanks to help from the binding.repl gem, you can start IRB (with irbtools) directly from your code:

binding.repl!

Debundle

Another way to activate irbtools in bundler projects is to add a debundle hack at the beginning of your ~/.irbrc file.

This is irbtools Light

When installing irbtools, some gems will not be installed to ensure Windows OS support, e.g. the bond gem for better auto-completion or the looksee gem. These are packaged into the irbtools-more gem. To use irbtools-more, you will need to change your .irbrc to:

require 'irbtools/more'

Included Gems and Libraries

wirb

Colorizes Ruby objects

hirb

Custom views for specific objects, e.g. tables for ActiveRecord

fancy_irb

Hash rockets 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

debugging

Print debugging helpers (q, mof, re)

ap

More print debugging (ap)

ruby_version

version

ruby_engine

engine

os

os

ruby_info

info

coderay

Colorizes Ruby code (colorize, ray)

methodfinder

malltalk like method finder for Ruby (mf)

ori

Object#ri

method_locator

Provides Object#mlp (improved version of Module#ancestors) and Object#methods_for(m) (get this method from all ancestors)

method_source

Object#src can be shown for Ruby methods

alias

Easily create shortcuts for your favorite methods, even when they are nested, saved in personal yaml file

boson

Command/task framework similar to rake and thor that opens your ruby universe to the commandline and irb

bond

irbtools-more: Better IRB tab-completion

looksee

irbtools-more: Great lookup path inspector: Object#l (Extended version of mof), also provides the ability to Object#edit methods.

fileutils (stdlib)

System utility methods: cd, pwd, ln_s, mv, rm, mkdir, touch

irbtools Methods (every_day_irb)

ls

Returns an array with the directory’s content

cat

Shortcut for File.read

rq

Shortcut for require library.to_s (allows concise syntax like rq: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 the terminal (system "clear")

session_history

Returns all issued commands as a string

irbtools also defines custom helper methods in combination with the loaded gems:

cd

Improves the cd that is already provided by FileUtils (try cd '-')

[version]Displays RubyVersion

engine

Displays RubyEngine

os

OS information

info

Aggregates information about your Ruby environment

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/lp

Alternative method name to trigger the looksee gem (irbtools-more)

Advanced tweaking

How to load more or less libraries

It is 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 do not want to load the default set of irbtools gems, you will have to use require 'irbtools/minimal' instead of configure.

You can use 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. The 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 pass a block as third argument, which gets executed after the library has completed loading (except for :autoload, in which case the code will executed directly on startup). You can modify the callbacks by using Irbtools.add_library_callback and Irbtools.replace_library_callback.

When adding a new library, you should firstly consider some way to load it via :autoload. If this is not possible, try loading via :thread. If that is not possible either, you will need to fallback to the default loading mechanism.

Troubleshooting: Unicode causes wrong display widths?

If you use double-width unicode characterss, you will need to paste the following snippet to your .irbrc file.

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

Welcome Message

The welcome message can be customized with Irbtools.welcome_message=

Web Console

Irbtools works well together with the amazing web-console!

J-_-L

Copyright © 2010-2014 Jan Lelis <janlelis.de> released under the MIT license.