Skip to content

Instantly share code, notes, and snippets.

View babney's full-sized avatar

Bill Abney babney

  • btwb
  • California Bay Area
View GitHub Profile
@babney
babney / transaction_screamer.rb
Created May 27, 2015 20:50
Log a stack trace for every transaction... action. Only for Mysql2adapter, could be fixed to work for others.
module TransactionScreamer
def begin_db_transaction
Rails.logger.debug "DB TRANSACTION STARTS"
Rails.logger.debug caller.join("\n")
super
end
def commit_db_transaction
Rails.logger.debug "DB TRANSACTION ENDS"
Rails.logger.debug caller.join("\n")
@babney
babney / timesorter.rb
Created May 2, 2012 23:13 — forked from anonymous/timesorter.rb
sorting arrays of "04/03/2012 06:10 PM 4,305 vp.js" and such
preresults = File.readlines("doiton.txt").reject{ |line| line =~ /\d{2}\/\d{2}\/\d{4} \d{2}:\d{2} [PA]M <DIR>|bytes|notes|^$|^01\/\d{2}\/2012|^02\/2[1234567]\/2012|^02\/[01][0123456789]\/2012| Directory of/}
midresults = preresults.map{|line| {:time => DateTime.strptime(line,'%m/%d/%Y %I:%M %p'), :string => line}}
sortedresults = midresults.sort_by{|hash| hash[:time]}
finalresults = sortedresults.map{|hash| hash[:string]}
File.write("rubout2.txt",finalresults.join)
@babney
babney / LikeRuby.rb
Created April 3, 2012 16:29
Like, Ruby
module LikeRuby
VALLEYGIRL = %w[omg so like totally right toootally stuff]
FRATGUY = %w[friggin fuckin dude man bro broheim broseph]
INTERNETS = %w[lol rofl teh ohai plz]
SNOOP = %w[yo homey homeboy sup dog shit girl ma biatch ho shiiit]
LOCAL = %w[wicked hella anyways]
MISC = %w[just hey yeah ok um uh ah actually something]
ALLSLANG = VALLEYGIRL + FRATGUY + INTERNETS + SNOOP + LOCAL + MISC
def method_missing(name, *args)
@babney
babney / method_cop.rb
Created October 31, 2011 21:58
MethodCop
module MethodCop
# Guard methods that have side effects with a callback that fires before the method is invoked.
# If the callback returns a "falsey" value, the method is halted and will not be called.
# The callback will return nil instead.
# if the method does not have side effects or you depend on its return value, you should NOT
# use this on that method! This will break the _hell_ out of design by contract.
# currently does not work with methods that accept blocks so be aware of that. Fixes, improvements,
# pull requests, and general "why on earth did you do this" notes are encouraged.
#TODO: DRY this up
@babney
babney / output.png
Created June 30, 2011 05:52
chunky pixelizer
output.png
@babney
babney / clipboard.rb
Created March 29, 2011 00:26
quick-and-dirty clipboard usage in ruby (OS X/Linux)
class Object
unless method_defined?(:clip) || !self.respond_to?(:to_s)
def clip # won't work in windows, but I doubt it'll ever be an issue...
if RUBY_PLATFORM.downcase.include?("darwin")
IO.popen('pbcopy', 'w+') do |clipboard|
clipboard.write(self.to_s)
end
elsif RUBY_PLATFORM.downcase.include?("linux") #requires xsel to be installed
IO.popen('xsel --clipboard --input', 'r+') do |clipboard|
clipboard.puts self.to_s