Skip to content

Instantly share code, notes, and snippets.

View joshnesbitt's full-sized avatar

Josh Nesbitt joshnesbitt

View GitHub Profile
initializer :after => :initialize_dependency_mechanism do
ActiveSupport::Dependencies.mechanism = :load
end
gem 'geokit'
def random
find(:first, :offset => (count * rand).to_i)
end
def tip(msg); puts; puts msg; puts "-"*100; end
#
# 30 Ruby 1.9 Tips, Tricks & Features:
# http://www.igvita.com/2011/02/03/new-ruby-19-features-tips-tricks/
#
tip "Upgrading to Ruby 1.9 is simple: rvm install 1.9.2 && rvm --default 1.9.2"
tip "Ruby 1.9 supports named captures in regular expressions!"
class LockFile
attr_accessor :path
def initialize(path="/tmp/lockfile.lock")
@path = path
if block_given?
lock!
yield
unlock!
end
function getParam(key)
{
var regex, values;
regex = new RegExp("[\\?&]" + key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]") + "=([^&]*)", "i");
values = regex.exec(unescape(window.location.href));
return (values == null) ? "" : values[1];
}
# Before:
@avatars = Avatar.order("avatars.name ASC, avatars.name ASC")
@avatars = @avatars.where(["avatars.name like ? or users.email like ? or users.name like ?", params[:term], params[:term], params[:term]]) unless params[:term].blank?
@avatars = @avatars.paginate(:page => params[:page], :per_page => 25)
# After
class Avatar
def self.find_by_something(something, order="name ASC")
['avatars.name LIKE ?', 'users.email LIKE ?', 'users.name LIKE ?'].inject(self.order(order).includes(:user)) do |assoc, cond|
module ExceptionCatcher
def with_exception_handling(silent=false, &block)
begin
yield block
rescue => e
unless silent
# send to hoptoad
puts "sending error somewhere"
end
module IntegerAssertions
def negative?
self < 0
end
def positive?
self > 0
end
var destination = document.getElementById("destinationTextbox");
var source = document.getElementById("sourceTextbox");
var destinationHandler = function(value){
return destination.value = value;
}
source.onchange = function(){
destinationHandler(this.value);
};