Skip to content

Instantly share code, notes, and snippets.

class Memcached
def fetch(key, options = {})
if !options[:force] && value = get(key)
value
elsif block_given?
value = yield
set(key, value)
value
end
end
class Memcached
def fetch(key)
if value = get(key)
value
elsif block_given?
begin
add(key, value = yield)
rescue Memcached::NotStored => e
value = get(key)
end
class Memcached
class Rails < ::Memcached
def fetch(key, options = {})
if value = get(key)
value
elsif block_given?
value = yield
begin
add(key, value)
# I'll forget this again, so I'm saving it somewhere.
# I needed to remove duplicates from a table.
# Here's how I did it.
category_uniques = Category.all.index_by(&:name).values
Category.all.each { |cat| cat.destroy unless category_uniques.include?(cat) }
# Edit: Reduced to two lines, for fun.
# question.rb
class Question < ActiveRecord::Base
validates_presence_of :body, :summary, :answer
validates_uniqueness_of :body
cattr_reader :per_page
@@per_page = 10
belongs_to :category
def application_packages
if deploy_stage == 'production'
# set up the cron task to reindex
cron 'rake sunspot:reindex', :ensure => :absent
cron 'rake cron',
:command => "/usr/bin/rake -f #{configuration[:deploy_to]}/current/Rakefile cron RAILS_ENV=#{ENV['RAILS_ENV']}",
:hour => 5,
:minute => 0
end
end
# Failing on line 3 when the cache hits
def find_categories
if (@categories = Rails.cache.read('all_categories')).nil?
@categories = Category.all(:order => 'name ASC')
Rails.cache.write('all_categories', @categories, :ttl => 1800)
end
end
# Solution:
def find_categories
# Used to graph results from autobench
#
# Usage: ruby autobench_grapher.rb result_from_autobench.tsv
#
# This will generate three svg & png graphs
require "rubygems"
require "scruffy"
require 'csv'
require 'yaml'
%p
%span.red This
,
%span.blue that
,
%span.green the other
.
ActionController::Integration::Session.class_eval do
def generic_url_rewriter
env = {
'REQUEST_METHOD' => "GET",
'QUERY_STRING' => "",
"REQUEST_URI" => "/",
"HTTP_HOST" => host,
"SERVER_PORT" => https? ? "443" : "80",
"HTTPS" => https? ? "on" : "off",
"rack.input" => "wtf"