This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Then /^open IRB$/ do | |
require 'irb' | |
original_argv = ARGV | |
ARGV.replace([]) | |
IRB.start | |
ARGV.replace(original_argv) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'bert' | |
require 'json' | |
require 'yajl' | |
require 'benchmark' | |
ITER = 1_000 | |
tiny = t[:ok, :awesome] | |
small = t[:ok, :answers, [42] * 42] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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'` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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} |