http://ruby-china.org/topics/16256
get https://github.com/mvj3/hangman/blob/master/data/words.txt
and compile the decision tree.json
ruby compile.rb
http://ruby-china.org/topics/16256
get https://github.com/mvj3/hangman/blob/master/data/words.txt
and compile the decision tree.json
ruby compile.rb
# http://zh.wikipedia.org/wiki/中文数字 | |
# http://china.younosuke.com/03_013.html | |
module ChineseNum | |
extend self | |
UPPER_ZERO = '零' | |
LOWER_ZERO = '〇' | |
UPPER_DIGITS = %w[壹 贰 叁 肆 伍 陆 柒 捌 玖].unshift nil | |
LOWER_DIGITS = %w[一 二 三 四 五 六 七 八 九].unshift nil |
# enable the template engine shortcuts | |
Slim::Engine.set_options shortcut: {'&' => {tag: 'input', attr: 'type'}, '#' => {attr: 'id'}, '.' => {attr: 'class'}}, sort_attrs: false | |
# enable stimulus shortcuts: | |
# - @controller="foo" | |
# - @target="bar" | |
# - @click...="#action" | |
module Slim | |
class Parser |
#! /usr/bin/env ruby | |
str = $stdin.read | |
if RUBY_VERSION < '1.9' | |
print str | |
exit | |
end | |
str.force_encoding 'utf-8' | |
str.gsub! /(\p{Han})([a-zA-Z0-9\(\)\[\]\{\}])/u do | |
"#$1 #$2" |
import java.security.*; | |
import javax.crypto.*; | |
import javax.crypto.spec.*; | |
import java.io.*; | |
class aes{ | |
static byte[] ptext; | |
static SecretKeySpec key; | |
public static void main(String[] args) throws Exception { |
# mac port installs bash_completion in /opt/local | |
if [ -f /opt/local/etc/bash_completion ]; then | |
. /opt/local/etc/bash_completion | |
# * | |
export GIT_PS1_SHOWDIRTYSTATE=1 | |
# $ | |
export GIT_PS1_SHOWSTASHSTATE=1 | |
# % | |
# export GIT_PS1_SHOWUNTRACKEDFILES=1 | |
export PS1='\[\e[32m\]λ \w\[\e[36m\]$(__git_ps1 " (%s)") [$(~/.rvm/bin/rvm-prompt i v)]\[\e[0m\]\n\[\e[32m\]→\[\e[0m\] ' |
# regexps to check if a string is pure chinese | |
class String | |
# 20k chars | |
CHINESE_UCS2 = /^(?: | |
[\x4e-\x9e][\x00-\xff] | |
|\x9f[\x00-\xa5] | |
)+$/xn | |
# 20k chars | |
CHINESE_UTF8 = /^(?: |
PokerHand = Struct.new :player, :cards | |
class PokerHand | |
Card = Struct.new :name, :value, :suit | |
class Card | |
def initialize name | |
super name, '23456789TJQKA'.index(name[0]), name[1] | |
end | |
def <=> other | |
value <=> other.value |
To test out the cache miss behavior of read operations, run a program building the index (30k entries) then another program building then reading the index (30k queries), and find their diff.
Here's how to generate the test script cmd.sh
3.times do |i|
%w[triez hash da].each do |ty|
puts "valgrind --tool=cachegrind --cachegrind-out-file=tmp/#{ty} ruby t.rb #{ty}"
puts "valgrind --tool=cachegrind --cachegrind-out-file=tmp/#{ty}.read ruby t.rb #{ty} read"
puts "cg_diff tmp/#{ty} tmp/#{ty}.read > tmp/#{ty}#{i}.diff"
#!/usr/bin/env ruby | |
name = File.basename ENV['TM_FILENAME'] | |
name = name.dup | |
name[/\.rb$/] = '' | |
names = [] | |
File.read(File.join(ENV['TM_PROJECT_DIRECTORY'], 'db/schema.rb')).scan(/( create_table "(\w+).*? end)/m) do |s, n| | |
names << [n.size, s] if n.start_with?(name) | |
end |