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 / format_big_number_with_comma.rb
Created October 14, 2011 09:33
逗号分割大整数
# http://www.justskins.com/forums/format-number-with-comma-37369.html
n.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse
@dchentech
dchentech / mongoid_logger.rb
Created October 27, 2011 02:07
Logging mongoid into Rails.logger
Mongoid.configure do |config|
config.master = Mongo::Connection.new(ENV['MONGOID_HOST'], ENV['MONGOID_PORT'], :logger => Rails.logger).db(ENV['MONGOID_DATABASE'])
end
@dchentech
dchentech / mongod.log
Created November 8, 2011 10:19
在mongodb上重建一个百万级数据量的索引就把进程给搞挂了
第一次在ruby客户端中途取消,第二次就华丽丽的down掉了
248300/1548508 16%
Tue Nov 8 17:59:10 Invalid access at address: 0x2aabdf7dbe24
Tue Nov 8 17:59:10 Got signal: 7 (Bus error).
Tue Nov 8 17:59:11 Backtrace:
0x8ad399 0x8ad970 0x3dc6a0eb10 0x7345f8 0x77b264 0x682625 0x6876e9 0x688b7f 0x689101 0x689370 0x688fed 0x689370 0x688fed 0x689370 0x688fed 0x689370 0x688fed 0x689370 0x688fed 0x689370
@dchentech
dchentech / assets.error
Created January 17, 2012 07:37
assets/application.css"
Started GET "/assets/application.css" for 127.0.0.1 at Tue Jan 17 15:26:06 +0800 2012
Served asset /application.css - 200 OK (42ms)
[2012-01-17 15:26:06] ERROR NoMethodError: undefined method `each' for nil:NilClass
/usr/local/rvm/gems/ree-1.8.7-2011.03/gems/rack-1.3.6/lib/rack/handler/webrick.rb:71:in `service'
/usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/server.rb:162:in `start'
/usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/server.rb:95:in `start'
@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"}
@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 / 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 / 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 / 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 / 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