Skip to content

Instantly share code, notes, and snippets.

View zliang-min's full-sized avatar

Gimi Liang zliang-min

View GitHub Profile
#!/usr/bin/env ruby
# --*-- encoding: UTF-8 --*--
# RPCFN: Ruby Fun (#4)
# @author 梁智敏(Gimi Liang) liang.gimi at gmail dot com
class Polynomial
COEFFICIENT_PATTERN = '%+d'.freeze
ANNOYING_COEFFICIENT = /\A([+-])1\Z/.freeze
# RPCFN #5: Mazes
# @author 梁智敏(Gimi Liang) [gimi.liang at gamil dot com]
# @date 2009/12/29
class Maze
START_POINT_MARKER = 'A'.freeze
END_POINT_MARKER = 'B'.freeze
INFINITE = (1.0 / 0.0).freeze
class Cell
WALL = '#'.unpack('c').first.freeze
# Factory girl, relaxed.
#
# Factory.define :user do |f|
# f.login 'johndoe%d' # Sequence.
# f.email '%{login}@example.com' # Interpolate.
# f.password f.password_confirmation('foobar') # Chain.
# end
#
# Factory.define :post do |f|
# f.user { Factory :user } # Blocks, if you must.
@zliang-min
zliang-min / sinatra_sequel.rb
Created January 27, 2010 15:04
A sample of using sequel and sinatra together.
require 'yaml'
require 'sinatra/base'
require 'sequel_core'
class MyApp < Sinatra::Base
configure do
DB = Sequel.connect YAML.load(File.read(__FILE__).split("__END__\n")[-1])[ENV['RACK_ENV'] || 'production']
Sequel::Mysql.convert_tinyint_to_bool = false
end
@zliang-min
zliang-min / gist:292786
Created February 2, 2010 16:25
add host condition to rails route
ActionController::Routing::Routes.draw do |map|
map.connect "/", :controller => "admin",
:conditions => { :subdomain => "admin" }
map.connect "/", :controller => "blog"
# ...
end
# jamisbuck/routing/routeset.rb
module JamisBuck
module Routing
@zliang-min
zliang-min / oshi.rb
Created July 8, 2010 14:11 — forked from anonymous/oshi.rb
a websocket example
require 'rubygems'
require 'eventmachine'
require 'em-websocket'
require 'json'
class Connection
attr_accessor :socket, :user_id
def initialize(socket, user_id)
@socket = socket
'''
redis_search.py
Written by Josiah Carlson July 3, 2010
Released into the public domain.
This module implements a simple TF/IDF indexing and search algorithm using
@zliang-min
zliang-min / pws.rb
Created November 2, 2010 17:06 — forked from janlelis/pws.rb
generate password
#!/usr/bin/env ruby
# pws
# see http://rbjl.net/41-tutorial-build-your-own-password-safe-with-ruby for more information
require 'rubygems' if RUBY_VERSION[2] == ?8
require 'openssl'
require 'fileutils'
require 'clipboard' # gem install clipboard
@zliang-min
zliang-min / webapp.rb
Created November 19, 2010 17:18 — forked from igrigorik/webapp.rb
Object webapp
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end
@zliang-min
zliang-min / stop_daemon.rb
Created December 10, 2010 07:16
stop daemons
pid_file = "someapp.pid"
pid = File.read pid_file
begin
Process.kill 'TERM', pid
rescue Errno::ESRCH
puts $!
end
require 'timeout'