~ brogers /.irbrc

Uses helper extension from err.the_blog (http://errtheblog.com/post/43), Rails Logger hack from Ryan Bates (http://railscasts.com/episodes/56), wirble (http://pablotron.org/software/wirble/), Ben Bleything's Shell-style History for irb (http://blog.bleything.net/pages/irb_history), boolean_golf.rb from Ruby By Example by Kevin C. Baird (http://www.nostarch.com/ruby.htm)
require 'rubygems'

#######################################
# helper extension from err.the_blog  #
#   http://errtheblog.com/post/43     #
#######################################

def Object.method_added(method)
  return super(method) unless method == :helper
  (class<<self;self;end).send(:remove_method, :method_added)

  def helper(*helper_names)
    returning $helper_proxy ||= Object.new do |helper|
      helper_names.each { |h| helper.extend "#{h}_helper".classify.constantize }
    end
  end

  helper.instance_variable_set("@controller", ActionController::Integration::Session.new)

  def helper.method_missing(method, *args, &block)
    @controller.send(method, *args, &block) if @controller && method.to_s =~ /_path$|_url$/
  end

  helper :application rescue nil
end if ENV['RAILS_ENV']

#######################################
#  Ryan Bates' Rails Logger hack      #
# (http://railscasts.com/episodes/56) #
#######################################
if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
  require 'logger'
  RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
end

############################################
#                wirble                    #
#  (http://pablotron.org/software/wirble/) #
############################################
require 'wirble'
# start wirble (with color)
Wirble.init
Wirble.colorize

#################################################
# Ben Bleything's Shell-style History for irb   #
# (http://blog.bleything.net/pages/irb_history) #
############################################################
# Adds shell-style history display and replay to irb.      #
# The magic happens in the h, h!, and hw methods.          #
# == Authors                                               #
# * Ben Bleything <ben@bleything.net>                      #
# == Copyright                                             #
# Copyright (c) 2007 Ben Bleything                         #
# This code released under the terms of the BSD license.   #
# == Version                                               #
#  $Id: history.rb 50 2007-07-30 18:55:09Z ben $           #
############################################################
# Lists the last how_many lines of history (defaults to 50).  Aliased to h.
def history( how_many = 50 )
    history_size = Readline::HISTORY.size

    # no lines, get out of here
    puts "No history" and return if history_size == 0

    start_index = 0

    # not enough lines, only show what we have
    if history_size <= how_many
        how_many  = history_size - 1
        end_index = how_many
    else
        end_index = history_size - 1 # -1 to adjust for array offset
        start_index = end_index - how_many 
    end

    start_index.upto( end_index ) {|i| print_line i}
    nil
end
alias :h  :history

# replay lines from history.  Aliased to h!
#
# h! by itself will replay the most recent line.  You can also pass in a
# range, array, or any mixture of the three.
#
# We subtract 2 from HISTORY.size because -1 is the command we just issued.
def history_do( *lines )
    lines = [Readline::HISTORY.size - 2] if lines.empty?

    to_eval = get_lines( lines )

    to_eval.each {|l| Readline::HISTORY << l}

    IRB.CurrentContext.workspace.evaluate self, to_eval.join(';')
end
alias :h! :history_do

# writes history to a named file.  This is useful if you want to show somebody
# something you did in irb, or for rapid prototyping.  Aliased to hw.
#
# Uses similar calling semantics to h!, that is, you can pass in fixnums,
# ranges, arrays, or any combination thereof.
def history_write( filename, *lines )
    File.open( filename, 'w' ) do |f|
        get_lines( lines ).each do |l|
            f.puts l
        end
    end
end
alias :hw :history_write

private

# simple getter to fetch from Readline
def get_line(line_number)
    Readline::HISTORY[line_number]
end

# the code what powers the line fetcherating.  Accepts an array and iterates
# over each entry, fetching that line from the history and placing it into a
# temporary array which is ultimately returned.
def get_lines( lines )
    out = []

    lines.each do |line|
        case line
        when Fixnum
            out << get_line( line )
        when Range
            line.to_a.each do |l|
                out << get_line( l )
            end
        end
    end

    return out
end

# prints out the contents of the line from history, along with a line number,
# if desired.
def print_line(line_number, show_line_numbers = true)
    print "[%04d] " % line_number if show_line_numbers
    puts get_line(line_number)
end
##################################################
# Adapted from Ruby By Example by Kevin C. Baird #
#       (http://www.nostarch.com/ruby.htm)       #
##################################################
# boolean_golf.rb
=begin rdoc
This is intended merely to add handy true? and false? methods to every
object. The most succinct way seemed to be declaring these particular
methods in this order. Note that to_b ("to Boolean") is an alias to
the true?() method.
=end
class Object
  def false?;!self;end
  def true?;!false?;end
  alias :to_b :true?
end  
Your Ad Here