Make IRB your usual shell

Blog ยป Make IRB your usual shell

Posted on 26 Apr 2009 15:45

Ruby is a very powerful language. Much more powerful and readable than bash, of course. Things that seem impossible become trivial with Ruby.

However, with pure Ruby, you don't have the ease of the shell. You can't just write cd, pwd, cat etc, etc. Are you sure? With a simple .irbrc file on your home directory you can! Improve your IRB or script/console now.

You will be able to write:

cd '/home/me'
cat '.irbrc'

Let's see an advanced example. I enjoy the ease of processing multiple directories while developing Rails:

Dir[**/plugins/*].each do |plugin_dir| inside_dir(plugin_dir) { `git status`} end

I am keeping .irbic up-to-date at GitHub. My .irbrc is as follows:

# IRB improvements: Command history, coloring, more shell like commands cd, pwd, dir, cat
# Use powerful shell commands
# dir
# dir '**/*.rb'
# cd 'app'
# pwd
# cat 'config/environment.rb'
# dir('vendor/plugins/*').each do |d| inside_dir(d) {`git pull`} end
# Developed by umur dot ozkul at gmail dot com
 
require 'pp'
require 'irb/completion'
IRB.conf[:PROMPT_MODE] = :SIMPLE
require 'rubygems'
require 'wirble'
require 'utility_belt'
Wirble.init
Wirble.colorize
 
require 'active_support'
require 'fileutils'
 
alias q exit
 
# See http://www.ruby-doc.org/core/classes/FileUtils.html
class Object
  def cd d, options={};      FileUtils.cd d, options; end
  def pwd;      FileUtils.pwd; end
  def dir d='*';  Dir[d]; end
  def cat file;    puts `cat #{file}`; end
  def ln_s old, new, options={};  FileUtils.ln_s old, new, options; end
  def mv file, new_file, options={};  FileUtils.mv file, new_file, options; end
  def rm list, options={};          FileUtils.rm list, options; end
  def mkdir dirname, options={};    FileUtils.mkdir dirname, options; end
  def touch list, options={};    FileUtils.touch list, options; end
 
  def inside_dir d, &block
  b = pwd
  begin
   cd d
   block.call
  ensure
   cd b
  end
  end
end

Here also other utilities are included: history, display coloring, message completion…

As you are now nicely equipped with Wirble an Utilitybelt, it's time to read more about them

If you like this page, please spread the word: diggdel.icio.usFacebook

You can contact me if you have questions or corrections.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License