Skip to content

Instantly share code, notes, and snippets.

View bachue's full-sized avatar

Bachue Zhou bachue

View GitHub Profile
@bachue
bachue / gist:5128173
Created March 10, 2013 11:09
A bug I found in ruby-v8
require 'v8'
v8 = V8::Context.new
r = v8.eval 'a = {"b": 310399688698392577}'
r[b].to_i # => 310399688698392576 (expected: 310399688698392577)
@bachue
bachue / gist:5188255
Last active December 15, 2015 02:38
My TSort Example
require 'tsort'
require 'pp'
Node = Struct.new :value, :before, :after
class Collection < Array
include TSort
alias :tsort_each_node :each
def tsort_each_child(current, &block)
def Object.superclasses
superclasses = superclass.superclasses if superclass.respond_to?(:superclasses)
superclasses ||= []
superclasses.unshift self
end
class A
class << self
def add(ele)
@array ||= []
@array << ele
end
def array
@array ||= []
end
jobs_per_process = 100
process_count = 10
User.find_in_batches(:batch_size => jobs_per_process * process_count, :conditions => "name like '%00'") do |group|
batches = group.in_groups(process_count)
batches.each do |batch|
Process.fork do
ActiveRecord::Base.establish_connection
batch.reject { |user| user.nil? }.
each {|user| puts user.name }
class A
def f() 'A#f' end
end
module M
def f() 'M#f' end
def g() 'M#g' end
def k() 'M#k' end
end
@bachue
bachue / filter.rb
Last active December 17, 2015 06:39
def run_filters(&blk)
m = Module.new do
extend self
@__filter_endpoint = blk
end
code = '@__filter_endpoint.call'
filter_methods = private_methods.grep(/^filter_/)
filter_methods.each do |meth|
m.send(:define_method, "__filter_#{meth}", method(meth))
end
@bachue
bachue / Errno in Ruby
Created May 26, 2013 06:59
Displayed by `Errno.constants.each {|errno| err = Errno.const_get(errno); puts "#{err}: #{err.new.message}" }`
Errno::ENOSYS: Function not implemented
Errno::EDESTADDRREQ: Destination address required
Errno::ESHUTDOWN: Can't send after socket shutdown
Errno::ENODEV: Operation not supported by device
Errno::ENOMEM: Cannot allocate memory
Errno::EMLINK: Too many links
Errno::EPROTO: Protocol error
Errno::ENETUNREACH: Network is unreachable
Errno::EIO: Input/output error
Errno::EMFILE: Too many open files
@bachue
bachue / pry_debug_info
Last active December 19, 2015 11:38
When I visit the file system by ruby httpd, it will crash when some Chinese file names in the folder. I debugged it here, the encoding of regular expression is ascii, but the Chinese file names encoding is UTF-8, so it could be the root cause.
From: /Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httputils.rb @ line 444 WEBrick::HTTPUtils._escape:
440: def _escape(str, regex)
441: begin
442: str.gsub(regex){ "%%%02X" % $1.ord }
443: rescue Exception
=> 444: require 'pry'; binding.pry
445: end
446: end