Skip to content

Instantly share code, notes, and snippets.

View zavakid's full-sized avatar
🍑
hungry

Zava zavakid

🍑
hungry
View GitHub Profile
@zavakid
zavakid / gist:1574212
Created January 7, 2012 08:39
rails resource control and path

Rails用這套慣例來大大簡化了路由設定。那程式該怎麼寫呢? 我們在config/routes.rb加入以下一行程式:

resources :events

如此就會自動建立四個命名路由(named routes),搭配四個HTTP動詞,對應到七個Actions。它的實際作用,就如同以下的設定:

get    '/events'          => "events#index",   :as => "events"
post   '/events'          => "events#create",  :as => "events"
get    '/events/:id'      => "events#show",    :as => "event"
put '/events/:id' => "events#update", :as => "event"
@zavakid
zavakid / gist:1697162
Created January 29, 2012 04:17
查看网络连接状态数
netstat -apnl | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"\t",state[key]}'
@zavakid
zavakid / gist:1715195
Created February 1, 2012 05:00
让终端输出颜色
#设置颜色的格式: \e[背景色;前景色;高亮m
# \033[背景色;前景色;高亮m
#恢复默认为 \e[0m
#其中背景色可以被以下数字替换
#第一个参数:
#0 透明(使用终端颜色),1 高亮 40 黑, 41 红, 42 绿, 43 黄, 44 蓝 45 紫, 46 青
#绿, 47白(灰)
#第二个参数:
#前景色(也就是文本的颜色)可以被以下数字替换
#30 黑 31 红, 32 绿, 33 黄, 34 蓝, 35 紫, 36 青绿, 37 白(灰)
@zavakid
zavakid / gist:1857221
Created February 18, 2012 03:34
javaassist insert some code before method
package com.zavakid.javassist.test;
public class Hello {
public void say(){
System.out.println("Hello world.");
}
}
//======================
package com.zavakid.javassist.test;
@zavakid
zavakid / gist:3045599
Created July 4, 2012 05:43
replace with regex and callback in PHP
<?php
function urlReplace($text, $replaceWith){
// set the callback function
$callback = function($match)use($replaceWith){
$replaced0 =preg_replace('/\{0\}/', $match[0], $replaceWith);
return preg_replace('/\{1\}/', toUpper($match[0]), $replaced0);
};
var_dump($callback);
return preg_replace_callback('/https?:\/\/\S+(?= |$)/',$callback, $text);
<?php
class StringRandomizer {
public function process( $text ) {
return preg_replace_callback('/\{(((?>[^\{\}]+)|(?R))*)\}/x', array( $this, 'replace' ), $text );
}
public function replace( $text ) {
$text = $this->process( $text[ 1 ] );
@zavakid
zavakid / SemaphoreTest.java
Created October 12, 2012 13:12
Semaphore hold the thread
package com.zavakid.concurrent;
import java.util.concurrent.Semaphore;
import org.junit.Test;
/**
* @author Zava 2012-10-12 下午1:06:15
* @version 1.0
*/
@zavakid
zavakid / gist:4045648
Created November 9, 2012 13:24
liquid tokenize
text = "{{asd}} sdfadsf afas {% a %}xxx{% enda %} asdfa asdf s af "
text.split(Liquid::TemplateParser)
# result:
[
"",
"{{asd}}",
" sdfadsf afas ",
"{% a %}",
"xxx",
@zavakid
zavakid / gist:5030465
Created February 25, 2013 15:13
exec a command with timeout
#/bin/bash
# 其实是使用了 perl 的 alarm
perl -e "alarm 10; exec @ARGV" "$@

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs