Skip to content

Instantly share code, notes, and snippets.

@baya
baya / rubber.rb
Created November 10, 2010 09:10
help method for visting a url
module Rubber
def self.try(diftimes = [3, 8, 2], options = { }, &block)
val = timeout(diftimes.shift) do
block.call
end
rescue options[:on] || Exception => e
Rails.logger.info("#{Time.now}:#{__FILE__}:#{__LINE__}}:#{e.backtrace.join("\n")}")
retry if diftimes.first
return ""
@baya
baya / ruby-blocks-as-dynamic-callbacks.rb
Created December 6, 2011 04:29
ruby blocks as dynamic callbacks
# From http://www.mattsears.com/articles/2011/11/27/ruby-blocks-as-dynamic-callbacks
class Proc
def callback(callable, *args)
self === Class.new do
method_name = callable.to_sym
define_method(method_name) { |&block| block.nil? ? true : block.call(*args) }
define_method("#{method_name}?") { true }
def method_missing(method_name, *args, &block) false; end
end.new
@baya
baya / README.markdown
Created January 21, 2012 09:15 — forked from AndorChen/README.markdown
Howto Depoly Rails App to VPS

#如何将 Rails 应用程序部署到 VPS 上

如果已经在 VPS 上假设好了 Rails 生产环境,接下来就可以将部署应用程序了。

###需要用到的工具


@baya
baya / lithp.rb
Created January 29, 2012 01:24 — forked from fogus/lithp.rb
class Lisp
def initialize
@env = {
:label => lambda { |(name,val), _| @env[name] = val },
:quote => lambda { |sexpr, _| sexpr[0] },
:car => lambda { |(list), _| list[0] },
:cdr => lambda { |(list), _| list.drop 1 },
:cons => lambda { |(e,cell), _| [e] + cell },
:eq => lambda { |(l,r), _| l == r },
:if => lambda { |(cond, thn, els), ctx| eval(cond, ctx) ? eval(thn, ctx) : eval(els, ctx) },
@baya
baya / server_install.md
Created February 27, 2012 14:28 — forked from bvajda/server_install.md
Postgresql 9.1 server on CentOS 6

install CentOS 6

yum update
yum upgrade

check the server time. sync the time to a time server if needed (service 'ntpd')

download and install Postgresql Server v9.1 rpm package:

rpm -Uvh http://yum.pgrpms.org/9.1/redhat/rhel-6-x86_64/pgdg-centos91-9.1-4.noarch.rpm

This should have installed the necessary server package: postgresql91-server.x86_64.

@baya
baya / gist:4022764
Created November 6, 2012 05:25
状态列表维护
# 用于维护状态列表
# Example:
#
# sm = StatusMapper.new({
# '购买彩票失败' => '-3',
# '默认' => ['-1', '未做任何处理'],
# '投注成功' => '0',
# '购买彩票成功' => '1'
# })
# sm['默认'].code #=> '-1'
@baya
baya / gist:4022774
Created November 6, 2012 05:26
确保程序能够独断运行
class Dogmatism
def initialize(process_name)
@process_name = process_name
@uuid = SecureRandom.uuid
end
def protect
begin
if File.exists? pidfile_path
@baya
baya / gist:4115408
Last active October 13, 2015 01:08
计算竞彩足球注数
# -*- coding: utf-8 -*-
# 竞彩注码算法
# 用例
# teams = [
# [1, '3', '0', '*'],
# [2, '3', '-'],
# [3, '3', '-'],
# [4, '3', '1', '*'],
# [5, '3', '0', '*'],
# [6, '3', '1', '-'],
@baya
baya / gist:4255760
Created December 11, 2012 03:48
计算排列组合
# 计算排列组合
module LotMath
extend self
# 阶乘
def F(n)
return 1 if n == 0
(1..n).inject(:*)
end
@baya
baya / gist:4344170
Last active December 9, 2015 23:28
状态管理工具v2.0
# 用于维护状态列表
# Example:
#
# sm = CodeNoteMapper.new({
# '-3' => '购买彩票失败',
# '-1' => ['默认', '未做任何处理'],
# '0' => '投注成功',
# '1' => '购买彩票成功'
# })
# sm['默认'].code #=> '-1'