Skip to content

Instantly share code, notes, and snippets.

@TimLang
TimLang / string.rb
Created April 16, 2014 07:34
simple similar number to number
class String
#only support number which greater than 1 and less than 100
def simple_similar_number_to_number
return 2 if self == '两'
ascii_numeric_chars = "0123456789"
utf_numeric_chars =
if self =~ /[[:digit:]]+/
"\uff10\uff11\uff12\uff13\uff14\uff15\uff16\uff17\uff18\uff19"
else
@TimLang
TimLang / renameProperty.js
Created March 19, 2014 07:06
javascript rename property
Object.prototype.renameProperty = function (oldName, newName) {
if (this.hasOwnProperty(oldName)) {
this[newName] = this[oldName];
delete this[oldName];
}
return this;
};
@TimLang
TimLang / array.rb
Last active August 29, 2015 13:57
ruby array uniq?
class Array
def uniq?
hash = {}
self.each do |e|
obj = block_given? ? yield(e) : e
return false if hash[obj]
hash[obj] = obj
end
hash.size == self.size
end
@TimLang
TimLang / array_custom_splite.rb
Last active August 29, 2015 13:57
array_custom_splite
class Array
def custom_split &block
tmp = self.each_with_index.inject({}) do |hash, (obj, index)|
if block.call(obj) || (index == 0)
hash[index] = []
else
hash[hash.keys.last] << obj
end
hash
end.map{|h| (h[0] = self[h[0]], h[1]=h[1]).flatten}
@TimLang
TimLang / cross_data_table.rb
Created March 4, 2014 08:47
cross data table
def build_courses
subjects = %w{语文 数学 英语 物理 化学 生物 历史 地理 文综 理综}
grades = {
1 => "七年级",
2 => "八年级",
3 => "九年级",
4 => "十年级"
}
terms = {
1 => "2013-2014学年",
@TimLang
TimLang / object.rb
Created February 13, 2014 07:31
serialize object to json without escaping
require 'active_support/all'
class Object
def to_json_without_escape
self.to_json.gsub(/\\u([0-9a-z]{4})/){|s| [$1.to_i(16)].pack("U")}
end
end
@TimLang
TimLang / server.bash
Created February 12, 2014 08:52
start a ruby server like webrick on some web source dir
ruby -run -e httpd . -p 8080
@TimLang
TimLang / code_statistics.rb
Created February 12, 2014 08:51
Code statistics
#ruby code_statistics.rb [<source dir> <test or spec dir>]
class CodeStatistics
def initialize(src, test)
@pairs = [
["Codes",src + "/"],
["Tests",test + "/"]
].collect { |name, dir| [ name, "#{dir}" ] }.select { |name, dir| File.directory?(dir) }
@statistics = calculate_statistics
@total = calculate_total if @pairs.length > 1
end
@TimLang
TimLang / roman_numerals_match.rb
Created January 16, 2014 10:08
regex for roman numerals
/(?:X(?:X(?:V(?:I(?:I?I)?)?|X(?:I(?:I?I)?)?|I(?:[VX]|I?I)?)?|V(?:I(?:I?I)?)?|I(?:[VX]|I?I)?)?|V(?:I(?:I?I)?)?|I(?:[VX]|I?I)?)/.match "-----IV----"
#<MatchData "IV">
@TimLang
TimLang / patten_test.rb
Last active January 3, 2016 10:49
split by "\d\.
"dsdsd1.xsds\ndsdsd\n2.dsds6dsd".split(/(?=\d\.(?!\d\.).+)/)
#["dsdsd", "1.xsds\ndsdsd\n", "2.dsds6dsd"]
#"ds32sdsd22.xsds\nd6sdsd\n11.dsds6dsd".split(/(?=[\d]{1,2}\.(?![\d]\.).+)/)
#(?=[\n\s]+[\d]+[\.](?![\d]{1,2}[\.]).+)
#aaaaa\n 1.--------\n\n 32.------\n\n 3.-----2.7+8。9----2\n 24.---