Create a gist now

Instantly share code, notes, and snippets.

##### 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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment