Skip to content

Instantly share code, notes, and snippets.

View dchentech's full-sized avatar
🎯
Focusing

David Chen dchentech

🎯
Focusing
View GitHub Profile
@dchentech
dchentech / bootstrap.css
Created November 30, 2012 05:46
pygments.rb在bootstrap样式下自动换行
pre {
white-space: pre; /* fix bootstrap conflict with pygments */
}
/* 代码框自动下滑到底部(footer)的原因是table标签未关闭,见: https://github.com/mvj3/pygments.rb/commit/e7ad93a8db7a4b6be94e8fc03661b88797c903e8#lib/pygments/popen.rb */
@dchentech
dchentech / pythonbrew.txt
Created November 26, 2012 07:21
CentOS安装Pygments.rb
系统默认的Python是2.4.3, 使用时报MentosError: Failed to get header错误。
看了下官方文档, pygments.rb需要2.5以上, 因此选择了和ruby的rvm类似的可以管理多版本python的https://github.com/utahta/pythonbrew。
运行默认推荐的 curl -kL http://xrl.us/pythonbrewinstall | bash , 终端提示如下错误:
$ CURLOPT_SSL_VERIFYPEER=false curl -kL http://xrl.us/pythonbrewinstall | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 229 100 229 0 0 299 0 --:--:-- --:--:-- --:--:-- 0
curl: (60) SSL certificate problem, verify that the CA cert is OK. Details:
@dchentech
dchentech / ActiveRecord_without_Rails.rb
Created November 20, 2012 06:57
在Rails外面使用ActiveRecord,并直接声明全部model
ActiveRecord::Base.establish_connection(YAML.load_file('./config/database.yml')['production'])
ActiveRecord::Base.connection.execute("show tables;").to_a.flatten.map do |table_name|
eval "class #{table_name.camelcase} < ActiveRecord::Base; self.table_name = :#{table_name}; end"
end
@dchentech
dchentech / sphinx.txt
Created November 20, 2012 06:33
undefined method `to_crc32' for 1:Fixnum
预备知识:如果对条件选择做索引的话,sphinx采用的数据结构均为非字符类型,碰到字符字段,在ThinkingSphinx都被默认经CRC32转换成标识唯一整数。
解决过程:ThinkingSphinx需要对表名做一个CRC32转化,具体在lib/thinking_sphinx/source/sql.rb的crc_column方法。该方法里为了对继承表进行兼容,里面会判断Rails里约定的inheritance_column是否也在表字段里(代码为 @model.column_names.include?(@model.inheritance_column)),如果有就把type字段也作为CRC32的参数。可是如果type为整数类型,那么它没有to_crc32方法,所以就报了undefined method `to_crc32' for 1:Fixnum 错误。
解决方案:给有整数type字段的model加一句 self.inheritance_column = :_type
@dchentech
dchentech / webapp.rb
Created November 8, 2012 10:03 — forked from igrigorik/webapp.rb
Inspired by @JEG2's talk at Rubyconf... Any ruby object, as a webapp! 'Cause we can. :-)
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
@dchentech
dchentech / c_sscanf_vs_ruby_regexp.rb
Created September 27, 2012 04:28
用C的sscanf和Ruby的正则表达式解析nginx日志性能对比
require 'rubygems'
require 'inline'
class ParseLogInC
inline do |builder|
builder.c <<-CODE
#include 'ruby.h'
#include 'stdio.h'
static VALUE nginx(VALUE line) {
VALUE ary = rb_ary_new();
@dchentech
dchentech / rent.rb
Created September 13, 2012 14:17
在豆瓣找房子ing
%w[open-uri nokogiri].map &method(:require)
@regexp = Regexp.new ENV['租房']
(0..19).to_a.map do |n|
html = Nokogiri open("http://www.douban.com/group/beijingzufang/discussion?start=#{n*25}")
html.css(".article table.olt tr td a").map {|i| {:title => i.attributes['title'].to_s, :href => i.attributes['href'].to_s}}.map do |h|
if h[:title].match @regexp
puts h.inspect
h[:href]
end
end
@dchentech
dchentech / dbsmongodblogmongod.log
Created March 26, 2012 10:36
mongodb在master模式下启动后无止尽创建local.*文件,原因在于启动参数设置的--oplogSize过大了,比如1048576,即1048576M,改成2048后就只创建了一个local.*
Mon Mar 26 06:23:21 [FileAllocator] allocating new datafile /dbs/mongodb/local.72, filling with zeroes...
Mon Mar 26 06:23:45 [FileAllocator] done allocating datafile /dbs/mongodb/local.72, size: 2047MB, took 23.744 secs
Mon Mar 26 06:23:45 [FileAllocator] allocating new datafile /dbs/mongodb/local.73, filling with zeroes...
Mon Mar 26 06:24:05 [FileAllocator] done allocating datafile /dbs/mongodb/local.73, size: 2047MB, took 20.625 secs
Mon Mar 26 06:24:05 [FileAllocator] allocating new datafile /dbs/mongodb/local.74, filling with zeroes...
Mon Mar 26 06:24:32 [FileAllocator] done allocating datafile /dbs/mongodb/local.74, size: 2047MB, took 26.592 secs
Mon Mar 26 06:24:32 [FileAllocator] allocating new datafile /dbs/mongodb/local.75, filling with zeroes...
Mon Mar 26 06:25:02 [FileAllocator] done allocating datafile /dbs/mongodb/local.75, size: 2047MB, took 29.55 secs
Mon Mar 26 06:25:02 [FileAllocator] allocating new datafile /dbs/mongodb/local.76, filling with zeroes...
Mon Mar 26 06:25:21 [FileAlloca
@dchentech
dchentech / parse_nginx_$time_local_in_ruby.rb
Created March 9, 2012 02:43
parse nginx $time_local in ruby
DateTime.strptime("08/Mar/2012:11:53:53 +0800", "%d/%b/%Y:%H:%M:%S %z")
# => Thu, 08 Mar 2012 11:53:53 +0800
@dchentech
dchentech / mongoid_mapreduce_unknown_out_specifier_db.rb
Created February 20, 2012 06:24
mongoid运行mapreduce出现unknown out specifier [db]错误
错误信息:Database command 'mapreduce' failed: (assertion: 'unknown out specifier [db]'; assertionCode: '13522'; errmsg: 'db assertion failure'; ok: '0.0').
解决方案:设置out现象里的db参数应该是一个字符串键,比如{"db" => "mapreduce_db", :replace => "tmp_mapreduce"}