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 Memcached | |
| def fetch(key, options = {}) | |
| if !options[:force] && value = get(key) | |
| value | |
| elsif block_given? | |
| value = yield | |
| set(key, value) | |
| value | |
| 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
| 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 |
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 Memcached | |
| class Rails < ::Memcached | |
| def fetch(key, options = {}) | |
| if value = get(key) | |
| value | |
| elsif block_given? | |
| value = yield | |
| begin | |
| add(key, value) |
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
| # 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. |
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
| # 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 |
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
| 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 |
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
| # 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 |
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
| # 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' |
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
| %p | |
| %span.red This | |
| , | |
| %span.blue that | |
| , | |
| %span.green the other | |
| . |
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
| 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" |