Giter Club home page Giter Club logo

irbtools's Introduction

irbtools

irbtools improves Ruby’s irb console. Unlike pry, you are still in your normal irb, but you have colors and lots of helpful methods. It’s designed to work out-of-the-box so there is no reason to not use it!

Setup

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 enable colors

  • On MacOS, you 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 notes

To 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 at the beginning of your ~/.irbrc.

Yesterday’s Ruby versions

irbtools is build for Ruby 1.9+. To last version working with 1.8.7, can be found in the legacy branch:

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 ensure Windows OS support, e.g. bond for 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/more'

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.

wirb

General colorizer for resulting Ruby objects

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

zucker

Print debugging helpers (q, Object#m, Object#d) and information pseudo-constants (Info, OS, RubyVersion, RubyEngine)

ap

Alternative for displaying Ruby objects

coderay

Colorizes Ruby code (colorize, ray)

paint

Enables easy access to terminal colors

methodfinder

“A Smalltalk like Method Finder for Ruby” (mf)

ori

Adds an Object#ri method

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

“A command/task framework similar to rake and thor that opens your ruby universe to the commandline and irb.”

bond

irbtools-more: Improves irb tab-completion

looksee

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

fileutils (stdlib)

Includes file system utility methods: cd, pwd, ln_s, mv, rm, mkdir, touch, … ;)

Irbtools methods

The 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 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 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 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)

  • 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-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=

Packages

There 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.

Copyright © 2010-2013 Jan Lelis <happycode.org> released under the MIT license.

J-_-L

irbtools's People

Contributors

amatsuda avatar aripollak avatar halan avatar janlelis avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.