Created
August 12, 2008 21:07
-
-
Save lackac/5148 to your computer and use it in GitHub Desktop.
My .irbrc file
This file contains hidden or 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
# load libraries | |
require 'rubygems' | |
require 'wirble' | |
# start wirble (with color) | |
Wirble.init | |
Wirble.colorize | |
# which from http://programmingishard.com/code/507 | |
module Kernel | |
# which { some_object.some_method() } => <file>:<line>: | |
def where_is_this_defined(settings={}, &block) | |
settings[:debug] ||= false | |
settings[:educated_guess] ||= false | |
events = [] | |
set_trace_func lambda { |event, file, line, id, binding, classname| | |
events << { :event => event, :file => file, :line => line, :id => id, :binding => binding, :classname => classname } | |
if settings[:debug] | |
puts "event => #{event}" | |
puts "file => #{file}" | |
puts "line => #{line}" | |
puts "id => #{id}" | |
puts "binding => #{binding}" | |
puts "classname => #{classname}" | |
puts '' | |
end | |
} | |
yield | |
set_trace_func(nil) | |
events.each do |event| | |
next unless event[:event] == 'call' or (event[:event] == 'return' and event[:classname].included_modules.include?(ActiveRecord::Associations)) | |
return "#{event[:classname]} received message '#{event[:id]}', Line \##{event[:line]} of #{event[:file]}" | |
end | |
# def self.crazy_custom_finder | |
# return find(:all......) | |
# end | |
# return unless event == 'call' or (event == 'return' and classname.included_modules.include?(ActiveRecord::Associations)) | |
# which_file = "Line \##{line} of #{file}" | |
if settings[:educated_guess] and events.size > 3 | |
event = events[-3] | |
return "#{event[:classname]} received message '#{event[:id]}', Line \##{event[:line]} of #{event[:file]}" | |
end | |
return 'Unable to determine where method was defined.' | |
end | |
alias_method :which, :where_is_this_defined | |
end | |
# String#to_proc from http://weblog.raganwald.com/2007/10/stringtoproc.html | |
class String | |
unless ''.respond_to?(:to_proc) | |
def to_proc &block | |
params = [] | |
expr = self | |
sections = expr.split(/\s*->\s*/m) | |
if sections.length > 1 then | |
eval sections.reverse!.inject { |e, p| "(Proc.new { |#{p.split(/\s/).join(', ')}| #{e} })" }, block && block.binding | |
elsif expr.match(/\b_\b/) | |
eval "Proc.new { |_| #{expr} }", block && block.binding | |
else | |
leftSection = expr.match(/^\s*(?:[+*\/%&|\^\.=<>\[]|!=)/m) | |
rightSection = expr.match(/[+\-*\/%&|\^\.=<>!]\s*$/m) | |
if leftSection || rightSection then | |
if (leftSection) then | |
params.push('$left') | |
expr = '$left' + expr | |
end | |
if (rightSection) then | |
params.push('$right') | |
expr = expr + '$right' | |
end | |
else | |
self.gsub( | |
/(?:\b[A-Z]|\.[a-zA-Z_$])[a-zA-Z_$\d]*|[a-zA-Z_$][a-zA-Z_$\d]*:|self|arguments|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"/, '' | |
).scan( | |
/([a-z_$][a-z_$\d]*)/i | |
) do |v| | |
params.push(v) unless params.include?(v) | |
end | |
end | |
eval "Proc.new { |#{params.join(', ')}| #{expr} }", block && block.binding | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment