Skip to content

Instantly share code, notes, and snippets.

View hooopo's full-sized avatar
🍏
I may be slow to respond.

Hooopo hooopo

🍏
I may be slow to respond.
View GitHub Profile
-module(mergesort).
-export([sort/1]).
% Usage:
% c(mergesort).
%
% mergesort:sort([2,7,55,4,30,6,7,99,1,100]).
% => [1,2,4,6,7,7,30,55,99,100]
sort([]) -> [];
# Build an inverted index for a full-text search engine with Redis.
# Copyright (C) 2009 Salvatore Sanfilippo. Under the BSD License.
# USAGE:
#
# ruby invertedindex.rb add somedir/*.c
# ruby invertedindex.rb add somedir/*.txt
# ruby search your query string
require 'rubygems'
require 'redis'
module Rack
class MemoryBloat
def initialize(app, logger)
@app = app
@logger = logger
end
def call(env)
memory_usage_before = memory_usage
result = @app.call(env)
@defunkt
defunkt / connection_fix.rb
Created November 19, 2009 20:00
MySQL server has gone away fix
# If your workers are inactive for a long period of time, they'll lose
# their MySQL connection.
#
# This hack ensures we re-connect whenever a connection is
# lost. Because, really. why not?
#
# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
#
# From:
# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/
@vvalgis
vvalgis / Capistrano tasks for starting unicorn.rb
Created May 7, 2010 08:13
Capistrano tasks for starting unicorn
set :rails_env, :production
set :unicorn_binary, "/usr/bin/unicorn"
set :unicorn_config, "#{current_path}/config/unicorn.rb"
set :unicorn_pid, "#{current_path}/tmp/pids/unicorn.pid"
namespace :deploy do
task :start, :roles => :app, :except => { :no_release => true } do
run "cd #{current_path} && #{try_sudo} #{unicorn_binary} -c #{unicorn_config} -E #{rails_env} -D"
end
task :stop, :roles => :app, :except => { :no_release => true } do
@xdite
xdite / gist:758319
Created December 29, 2010 07:56
Ruby / Rails Convention of Techbang

Rails 開發注意要點

About Ruby Syntax

  • 編輯器設定 soft tab (space=2),以 2 格空白符號做為程式內縮距離(不分語言)。
  • 函式如果只有一個參數,就不強制打()
  • 函式如果有二個以上的參數,通通都要有 ()
    • (避免發生奇怪的paser bug跟保持專案一致性)
  • 字串限定用雙引號包覆
  • 善用 "#{str1} #{str3} " 等字串改寫技巧取代不需要的字串加法。
# Author: Pieter Noordhuis
# Description: Simple demo to showcase Redis PubSub with EventMachine
#
# Update 7 Oct 2010:
# - This example does *not* appear to work with Chrome >=6.0. Apparently,
# the WebSocket protocol implementation in the cramp gem does not work
# well with Chrome's (newer) WebSocket implementation.
#
# Requirements:
# - rubygems: eventmachine, thin, cramp, sinatra, yajl-ruby
@foxzool
foxzool / rails3template.rb
Created January 13, 2011 10:24
my rails3 template
require "thor/shell"
say("Modifying a new Rails app ...", :yellow)
#----------------------------------------------------------------------------
# Configure
#----------------------------------------------------------------------------
=begin
unless options[:database] == 'sqlite3'
username = ask("What's your database username[root]")
username = 'root' if username.blank?
@eddorre
eddorre / Rails Migrations Post Merge Git Commit Hook
Created April 18, 2011 21:58
Rails Migrations Post Merge Git Commit Hook
Get notifications when there are migrations that need to be run in a Rails project:
https://img.skitch.com/20110418-c9y3ttap3tai7frc43f38qtb8e.jpg
In your Rails project edit the .git/config file to add the following:
[rails]
automigrate = false
automigrateforegroundcolor = yellow
automigratebackgroundcolor = black
@karmi
karmi / elasticoverflow.rb
Created May 3, 2011 09:32
Importing and searching RSS with ElasticSearch and Tire
# =======================================================
# Importing and searching RSS with ElasticSearch and Tire
# =======================================================
#
# This script downloads, parses and indexes Stackoverflow RSS feed with ElasticSearch
# via the [Tire](https://github.com/karmi/tire) Rubygem.
#
# Requirements
# ------------
#