Skip to content

Instantly share code, notes, and snippets.

@unixmonkey
Created December 11, 2009 21:56

Revisions

  1. unixmonkey created this gist Dec 11, 2009.
    82 changes: 82 additions & 0 deletions .irbrc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    require 'pp'
    require 'irb/completion'
    require 'rubygems'
    require 'wirble'
    require 'hirb'

    Wirble.init
    Wirble.colorize
    IRB.conf[:AUTO_INDENT]=true

    # Save IRB History to file and use it
    HISTFILE = "~/.irb_history" unless Module.constants.member? "HISTFILE"
    MAXHISTSIZE = 100 unless Module.constants.member? "MAXHISTSIZE"
    begin
    if defined? Readline::HISTORY
    histfile = File::expand_path( HISTFILE )
    if File::exists?( histfile )
    lines = IO::readlines( histfile ).collect {|line| line.chomp}
    puts "Read %d saved history commands from %s." % [ lines.nitems, histfile ] if $DEBUG || $VERBOSE
    Readline::HISTORY.push( *lines )
    else
    puts "History file '%s' was empty or non-existant." % histfile if $DEBUG || $VERBOSE
    end

    Kernel::at_exit {
    lines = Readline::HISTORY.to_a.reverse.uniq.reverse
    lines = lines[ -MAXHISTSIZE, MAXHISTSIZE ] if lines.nitems > MAXHISTSIZE
    $stderr.puts "Saving %d history lines to %s." % [ lines.length, histfile ] if $VERBOSE || $DEBUG
    File::open( histfile, File::WRONLY|File::CREAT|File::TRUNC ) { |ofh|
    lines.each { |line| ofh.puts line }
    }
    }
    end
    end

    # When Rails is loaded, show the project name in the prompt
    if rails_env = ENV['RAILS_ENV']
    rails_root = File.basename(Dir.pwd)
    prompt = "#{rails_root}[#{rails_env}]"
    IRB.conf[:PROMPT] ||= {}
    IRB.conf[:PROMPT][:RAILS] = {
    :PROMPT_I => "#{prompt}> ",
    :PROMPT_S => "#{prompt}* ",
    :PROMPT_C => "#{prompt}? ",
    :RETURN => "=> %s\n"
    }
    IRB.conf[:PROMPT_MODE] = :RAILS

    # Called after the irb session is initialized and Rails has
    # been loaded (props: Mike Clark).
    IRB.conf[:IRB_RC] = Proc.new do
    # Shows what's logged in realtime (beats tailing the log in another terminal)
    # ActiveRecord::Base.logger = Logger.new(STDOUT)
    # Alias User[3] for User.find(3)
    # ActiveRecord::Base.instance_eval { alias :[] :find }
    end
    end

    class Object
    # Return a list of methods defined locally for a particular object. Useful
    # for seeing what it does whilst losing all the guff that's implemented
    # by its parents (eg Object).
    def local_methods(obj = self)
    (obj.methods - obj.class.superclass.instance_methods).sort
    end
    end

    # From http://blog.evanweaver.com/articles/2006/12/13/benchmark/
    # Call benchmark { } with any block and you get the wallclock runtime
    # as well as a percent change + or - from the last run
    def benchmark
    cur = Time.now
    result = yield
    print "#{cur = Time.now - cur} seconds"
    puts " (#{(cur / $last_benchmark * 100).to_i - 100}% change)" rescue puts ""
    $last_benchmark = cur
    result
    end


    %w{init colorize}.each { |str| Wirble.send(str) } # colorize the output
    Hirb::View.enable # when returning AR:Base objects, use mysql table-like view