Skip to content

Instantly share code, notes, and snippets.

@coty
Forked from adamcrown/.irbrc
Created November 29, 2011 04:22
Show Gist options
  • Save coty/1403396 to your computer and use it in GitHub Desktop.
Save coty/1403396 to your computer and use it in GitHub Desktop.
My Bundler proof .irbrc file including wirble, awesome_print, hirb, console logging and route helpers
require 'rubygems' unless defined? Gem # rubygems is only needed in 1.8
def unbundled_require(gem)
loaded = false
if defined?(::Bundler)
Gem.path.each do |gems_path|
gem_path = Dir.glob("#{gems_path}/gems/#{gem}*").last
unless gem_path.nil?
$LOAD_PATH << "#{gem_path}/lib"
require gem
loaded = true
end
end
else
require gem
loaded = true
end
raise(LoadError, "coudln't find #{gem}") unless loaded
loaded
end
def load_gem(gem, &block)
begin
if unbundled_require gem
yield if block_given?
end
rescue Exception => err
warn "Couldn't load #{gem}: #{err}"
end
end
# Highlighting and other features
load_gem 'wirble' do
Wirble.init
Wirble.colorize
end
# Improved formatting for objects
load_gem 'awesome_print'
# Improved formatting for collections
load_gem 'hirb' do
Hirb.enable
end
# Show log in Rails console
if defined? Rails
require 'logger'
if Rails.version =~ /^2\./ # Rails 2.3
Object.const_set('RAILS_DEFAULT_LOGGER', Logger.new(STDOUT))
else # Rails 3
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
end
# Enable route helpers in Rails console
if defined? Rails
include Rails.application.routes.url_helpers
default_url_options[:host] = 'localhost'
default_url_options[:port] = 3000
end
# Benchmarking helper (http://ozmm.org/posts/time_in_irb.html)
if defined? Benchmark
def time(times = 1)
ret = nil
Benchmark.bm { |x| x.report { times.times { ret = yield } } }
ret
end
end
# Random time method
class Time
def self.random(from=Time.at(0), to=Time.now)
Time.at rand(to.to_f - from.to_f) + from.to_f
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment