Skip to content

Instantly share code, notes, and snippets.

View jqr's full-sized avatar

Elijah Miller jqr

View GitHub Profile
class ActiveRecord::Base
def self.soft_destroyable(column = :destroyed_at)
define_method(:destroy_without_callbacks) do
update_attribute(column, Time.now)
end
named_scope :destroyed, :conditions => ["NOT #{column} IS NULL", column]
named_scope :not_destroyed, :conditions => ["#{column} IS NULL", column]
end
end
var grouped_products = [[]];
var group_index = 0;
_.each(products, function(product, index) {
if (index % 3 == 0) {
group_index++;
grouped_products[group_index] = []
}
grouped_products[group_index].push(product);
});
@jqr
jqr / gist:223107
Created October 31, 2009 15:29 — forked from bkeepers/gist:223062
// easy refresh-css keybinding to alt-w
// alt-r was taken in IE, so consider this a CSS Weefresh
// original code from http://paulirish.com/2008/how-to-iterate-quickly-when-debugging-css/
$(document).keyup(function(e){
if (e.which == 87 && e.altKey) {
$('link').each(function() {
if((/stylesheet/i).test(this.rel) && this.href) {
var href = this.href.replace(/(&|\?)forceReload=\d+/,'');
this.href = href + ((/\?/).test(href) ? '&' : '?' ) + 'forceReload=' + new Date;
Then /^open IRB$/ do
require 'irb'
original_argv = ARGV
ARGV.replace([])
IRB.start
ARGV.replace(original_argv)
end
@jqr
jqr / bertmark.rb
Created October 23, 2009 12:40 — forked from brianmario/bertmark.rb
require 'rubygems'
require 'bert'
require 'json'
require 'yajl'
require 'benchmark'
ITER = 1_000
tiny = t[:ok, :awesome]
small = t[:ok, :answers, [42] * 42]
@jqr
jqr / snippet.rb
Created September 27, 2009 18:07
prepend_before_filter :set_session_cookie_options
# A hack for using expire_after in Rails before 2.3
def set_session_cookie_options
self.class.session_options[:session_expires] = self.class.session_options[:expire_after].from_now
end
#!/usr/bin/env ruby
home = File.expand_path('~')
Dir["#{home}/Library/Application Support/TextMate{,/Pristine Copy}/Bundles/*/.git"].each do |bundle|
dir = bundle.gsub(/\.git/, '')
puts "Updating #{File.basename(dir)}"
Dir.chdir(dir) { `git pull` }
end
`osascript -e 'tell app "TextMate" to reload bundles'`
class User < ActiveRecord::Base
def score=(value)
super(value * 1_000_000) # pinball scores do it, why not Users?
end
end
describe User do
describe "creating a new user" do
it "should multiply the score by some ridiculous amount" do
user = User.new(:score => 5)
require 'benchmark'
count = 10_000_000
Benchmark.bm(10) do |bm|
bm.report('control') { count.times { [1,2,3,4,5,6] } }
bm.report('splat') { count.times { [1,2,3, *[4,5,6]] } }
bm.report('plus') { count.times { [1,2,3] + [4,5,6] } }
bm.report('flatten') { count.times { [1,2,3, [4,5,6]].flatten } }
end
# In ruby 1.9
{:one => 1, :two => 2}.select {|k,v| k == :one} #=> {:one=>1}
{:one => 1, :two => 2}.reject {|k,v| k == :one} #=> {:two=>2}