public
Last active

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
##### FILE : lib/words.rb
 
# Gives you N random word(s) over a certain length
# Only works on unixlike systems where this file exists
class Words
# DICT_PATH = '/usr/share/dict/words'
# DICT_SIZE = 234936
def self.random_word(words = 1, minlength = 6)
@@dict_all ||= File.open("/usr/share/dict/words").readlines.map{|l| l.chomp}
@@dict_size ||= @@dict_all.length
word_ary = []
name = ""
words.times do
begin
# name = %x[sed -n '#{rand(DICT_SIZE)} {p;q;}' '#{DICT_PATH}'].chomp
name = @@dict_all[rand(@@dict_size)]
end until name.length > minlength
word_ary << name
end
word_ary * " "
end
end
 
 
 
##### FILE : lib/debug.rb
 
module Debug
# LOGGER = Logger.new(STDOUT)
# LOGGER.level = Logger::DEBUG
# LOGGER = Rails.logger
 
def log_all
log_ /.*/
end
def log_ regex
raise "parameter must be of type Regexp" unless regex.is_a? Regexp
mod = Module.new
extend mod
class_str = to_s
methods.each do |m|
if !!m.to_s.match(regex) && !(m.to_s =~ /^log_/)
mod.send(:define_method, m) do |*a|
start = Time.now
gn = Words.random_word
Rails.logger.debug "***>>Called #{class_str}##{m} (ID:#{gn})#{' with arguments:' if !a.blank?}"
Rails.logger.debug a.map{|arg| arg.to_yaml rescue arg.to_s}.join("\n") if !a.blank?
result = super
Rails.logger.debug "***<<Returned #{class_str}##{m} (ID:#{gn}) in: #{Time.now - start}s"
result
end
end
end
end
end
 
class Object
include Debug
end
 
 
#### USE:
# just call "log_all" in the class you want to analyze and you will get a trace of ALL method calls, with arguments, and return times.
# You can also narrow down to particular methods by passing a regex into the "log_" call (note underscore to prevent collision with any existing log methods).

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.