Created
March 26, 2012 09:15
-
-
Save vjt/2204094 to your computer and use it in GitHub Desktop.
My Pry configuration file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim: ft=ruby | |
# -*- pryrc -*- | |
# | |
# Learn more about pry: https://github.com/pry/pry | |
# | |
# Allows you to just run "pry" inside a Rails app directory and get | |
# everything loaded as rails c does. Inside a Bundler directory does | |
# what bundle console does. | |
# | |
# Loads also Hirb and ruby-debug, if present, and a ~/.rubyrc with | |
# other useful methods - I use that file to share methods between | |
# irb and pry. | |
# | |
# - vjt@openssl.it | |
# | |
# Try to load Hirb | |
# | |
begin | |
require 'hirb' | |
Hirb.enable | |
old_print = Pry.config.print | |
Pry.config.print = proc do |output, value| | |
Hirb::View.view_or_page_output(value) || old_print.call(output, value) | |
end | |
rescue LoadError | |
puts 'Failed to load Hirb!' | |
end | |
# Try to load ruby-debug | |
# | |
begin | |
require 'ruby-debug' | |
rescue LoadError | |
puts 'Debugger not available' | |
end | |
load "#{ENV['HOME']}/.rubyrc" | |
# Load Rails, if applicable | |
# | |
require 'pathname' | |
root = Pathname(Dir.getwd) | |
if (rails = root.join 'config', 'environment.rb').exist? | |
start = Time.now.to_f | |
print "\033[1m** Loading \033[1;31mRails\033[0m... " | |
require rails | |
puts "\033[1mdone\033[0m: \033[1;33m#{Rails.version}\033[0m (\033[1;32m#{Rails.env}\033[0m)" | |
# Rails helpers | |
require 'rails/console/app' | |
require 'rails/console/helpers' | |
# Test helpers | |
if Rails.env.test? | |
[ root.join('spec', 'spec_helper.rb'), | |
root.join('test', 'test_helper.rb') | |
].each do |helper| | |
if helper.exist? | |
print "\033[1m** Loading \033[1;31m#{helper.basename}\033[0m... " | |
require helper | |
puts "\033[1mdone\033[0m" | |
end | |
end | |
end | |
puts "\033[1m** Booted in \033[1;31m#{(Time.now.to_f - start).round(3)}\033[1m\033[0msecs" | |
elsif root.join('Gemfile').exist? | |
start = Time.now.to_f | |
print "\033[1m** Loading \033[1;31mBundler\033[0m... " | |
require 'bundler' | |
Bundler.require | |
puts "\033[1mdone" | |
puts "\033[1m** Booted in \033[1;31m#{(Time.now.to_f - start).round(3)}\033[1m\033[0msecs" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim:ft=ruby | |
# -*- rubyrc -*- | |
# | |
# Useful additions to the irb/pry REPL console | |
# | |
# - vjt@openssl.it | |
# | |
# Easily print methods local to an object's class (thx @paozac) | |
# | |
class Object | |
def local_methods | |
(methods - Object.instance_methods).sort | |
end | |
def my_methods | |
(methods - self.class.superclass.instance_methods).sort | |
end | |
def sorted_methods | |
methods.sort | |
end | |
end | |
# Quick & easy benchmarking | |
# | |
# > bench { String.new } | |
# 1000 iterations: 0.001150 (0.000001 per iteration) | |
# | |
module Kernel | |
def bench(n = 1000, &block) | |
raise ArgumentError, 'What should I benchmark?' unless block | |
require 'benchmark' | |
print "#{n} iterations: " | |
time = Benchmark.measure { n.times(&block) }.real | |
puts "%.06f (%.06f per iteration)" % [time, time / n] | |
rescue LoadError | |
puts "Benchmark is not available in this session" | |
end | |
end | |
# Require a gem from GEM_PATH when it's not included | |
# in the bundle (e.g. you wanna load ruby-debug from | |
# your production server console.. while wearing the | |
# pink hat: www.bnj.com/cowboy-coding-pink-sombrero) | |
# | |
def require_local_gem(name) | |
if $:.grep(/#{name}/) | |
puts "Gem #{name} already loaded" | |
return | |
end | |
local_path = ENV['GEM_PATH'] && ENV['GEM_PATH'].split(':').last | |
gem_path = Dir["#{local_path}/#{name}*"].last | |
unless gem_path | |
puts "Local gem #{name} not found!" | |
return | |
end | |
$: << "#{gem_path}/lib" | |
end | |
# EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment