Skip to content

Instantly share code, notes, and snippets.

@jgrevich
Last active December 16, 2015 13:19
Show Gist options
  • Save jgrevich/5441060 to your computer and use it in GitHub Desktop.
Save jgrevich/5441060 to your computer and use it in GitHub Desktop.
my .irbrc
require 'rubygems'
require 'ap' # Awesome Print
require 'net-http-spy' # Print information about any HTTP requests being made
%w{wirble what_methods}.each { |lib| require lib }
# %w{what_methods}.each { |lib| require lib }
%w{init colorize}.each { |message| Wirble.send(message) }
# automate creating pasties
# clipboard code: http://project.ioni.st/post/1334#snippet_1334
require 'net/http'
def pastie
url = URI.parse("http://pastie.caboo.se/pastes/create")
parameters = {}
IO.popen('pbpaste') do |clipboard|
parameters["paste[body]"] = clipboard.read
end
parameters["paste_parser"] = "ruby"
parameters["paste[authorization]"] = "burger"
pastie_url = Net::HTTP.post_form(url, parameters).body.match(/href="([^\"]+)"/)[1]
IO.popen('pbcopy', 'w+') do |clipboard|
clipboard.write(pastie_url)
end
pastie_url
end
alias :pst :pastie
Bond.start
require 'hirb/import_object'
Hirb.enable({:width => 197, :height => 65, :fields => 24})
extend Hirb::Console
# auto indent, _ special var
IRB.conf[:USE_READLINE] = true
IRB.conf[:AUTO_INDENT] = false
IRB.conf[:EVAL_HISTORY] = 2048
# Save History between irb sessions
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 2048
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
# verbosity: http://groups.google.com/group/ruby-talk-google/browse_thread/thread/9c1febbe05513dc0
module IRB
def self.result_format
conf[:PROMPT][conf[:PROMPT_MODE]][:RETURN]
end
def self.result_format=(str)
result_format.replace(str)
end
def self.show_results
self.result_format = "=> %s\n"
end
def self.hide_results
self.result_format = ''
end
end
def verbose
IRB.show_results
end
alias :v :verbose
def quiet
IRB.hide_results
end
alias :q :quiet
alias :x :exit
# verbosity: http://weblog.jamisbuck.org/2007/1/8/watching-activerecord-do-it-s-thing
# Marcel said they toyed with making this the console default on core
def log
ActiveRecord::Base.clear_active_connections!
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
# history
# http://blog.bleything.net/pages
# http://gilesbowkett.blogspot.com/2007/06/irbrc-modifications.html
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
# -2 because -1 is ourself
def history_do(lines = (Readline::HISTORY.size - 2))
irb_eval lines
end
alias :h! :history_do
def history_write(filename, lines)
file = File.open(filename, 'w')
get_lines(lines).each do |l|
file << "#{l}\n"
end
file.close
end
alias :hw :history_write
module DynamicPrompt
def self.apply!
IRB.conf[:PROMPT][:INFORMATIVE] = {
:PROMPT_I => ">>".tap {|s| def s.dup; gsub('>>', DynamicPrompt.normal); end },
:PROMPT_S => "\e[0;35m%n\e[0m ",
:PROMPT_C => "\e[0;35m%n\e[0m> ",
:RETURN => "\e[1;35m=>\e[0m %.2048s\n"
}
IRB.conf[:PROMPT_MODE] = :INFORMATIVE
end
def self.normal
color1 = "\e[0;35m%n \e[35m[\e[1;35m"
color2 = "\e[0;35m]::\e[1;34m"
color3 = "\e[0;35m]\e[0m>"
color1 + current_ruby + color2 + cwd + color3
end
private
def self.current_ruby
@@current_ruby ||= `rvm current`.strip
end
def self.cwd
if Dir.pwd == '/'
'/'
elsif Dir.pwd == ENV['HOME']
'~'
else
Dir.pwd.split('/').last
end
end
end
DynamicPrompt.apply!
private
def get_line(line_number)
Readline::HISTORY[line_number]
end
def get_lines(lines = [])
return [get_line(lines)] if lines.is_a? Fixnum
out = []
lines = lines.to_a if lines.is_a? Range
lines.each do |l|
out << Readline::HISTORY[l]
end
return out
end
def print_line(line_number, show_line_numbers = true)
print line_number.to_s + ": " if show_line_numbers
puts get_line(line_number)
end
def irb_eval(lines)
to_eval = get_lines(lines)
to_eval.each {|l| Readline::HISTORY << l}
eval to_eval.join("\n")
end
# http://gilesbowkett.blogspot.com/2006/12/smalltalk-cleverness-translated-into.html
# http://gilesbowkett.com/blog_code_samples/122906_seaside_rails/controller.txt
def grep_classes(search_term)
classes = []
ObjectSpace.each_object {|obj| classes << obj.name if obj.is_a? Class and not obj.name.blank?}
classes.find_all {|klass| klass.downcase.include? search_term.downcase}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment