Skip to content

Instantly share code, notes, and snippets.

View luikore's full-sized avatar

luikore luikore

View GitHub Profile
@luikore
luikore / chinese_num.rb
Last active September 29, 2022 07:50
To Chinese Number
# 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
@luikore
luikore / slim_stimulus.rb
Created June 30, 2020 09:39
Enable some shortcuts for stimulus: @controller, @target, @click
# 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
@luikore
luikore / space_en_ch.rb
Created June 13, 2013 17:24
textmate command to add space between english and chinese
#! /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"
@luikore
luikore / aes.java
Created December 2, 2013 09:17
AES, ruby vs java
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 {
@luikore
luikore / bash prompt
Created March 25, 2011 04:13
lambda-like bash prompt with git / rvm hints
# 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\] '
@luikore
luikore / gist:149493
Created July 18, 2009 09:55
chinese string
# 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 = /^(?:
@luikore
luikore / poker_hands.rb
Last active September 1, 2016 03:29
Ruby Solution to Poker Hands Dealer Problem: https://ruby-china.org/topics/30918
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
@luikore
luikore / hat-cache-miss.md
Last active May 5, 2016 15:22
Comparing cache-miss of triez, fast_trie and hash reads

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"
@luikore
luikore / cmd.rb
Last active December 19, 2015 04:39
A textmate command to show model fields so model can be cleaner without annotates
#!/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