This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://melborne.github.io/2012/07/12/object-repeat-take-care-of-sequence/ | |
class Object | |
def repeat(init=true, &blk) | |
x = self | |
Enumerator.new do |y| | |
y << x if init | |
loop { y << (x = yield x) } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
key_space_limit = 65536 | |
class RangeError(Exception): | |
pass | |
class KeySpaceConstrainedParams(object): | |
def __init__(self, limit=key_space_limit): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def txt_to_utf(file) | |
open(file) do |f| | |
s = f.read | |
#shift_jis 必须写在前面,因为被误判为gbk的概率很高 | |
x = %w(shift_jis gbk big5) | |
begin | |
u = s.dup.encode("utf-8",x.shift) | |
rescue Encoding::UndefinedConversionError,Encoding::InvalidByteSequenceError => ex | |
if x.empty? | |
if s.chr.ord.to_s(16) == "efbb" |