Tuesday, December 25, 2007

Google chart api revoke using ruby and extjs

ruby api of google chart: http://gchart.rubyforge.org/gchart/

Extjs Generator for the Chart Server API: http://almaer.com/chartmaker/

利用 Ruby 进行 UDP 通信

收信端

require 'socket'
u1 = UDPSocket.open()
u1.bind("0.0.0.0", 10000)
p u1.recvfrom(65536)

发信端

require 'socket'
u2 = UDPSocket.new()
u2.connect('localhost', 10000)
u2.send('Hello world!' , 0)

接收结果
["Hello world!", ["AF_INET", 32818, "localhost", "127.0.0.1"]]

原文: http://d.hatena.ne.jp/emergent/20071225/1198510691

Monday, December 24, 2007

install vim doc

在Vim中运行:
:help =
提示错误类似:
E433:No tags file
E149:Sorry, no help for =
Press ENTER or type command to continue

在Vim下运行:
:helptags ~/.vim/doc
(或者是 :helptags 安装目录下的/vim7/doc)

安装了新的插件也是一样要运行一下此命令,可单独指定help文件名

[Mysql] Row size too large

Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs

一个表有130个varchar(255)字段,GBK编码,建表报以上错误,原因如下:
130 * 255 * 2 = 66560 > 66535 (GBK 2字节)
表字段长度总和Mysql有限制.如果表是utf-8的话,按3字节计算.

Friday, December 21, 2007

隐藏Gvim菜单和工具栏

在~/.vimrc(_vimrc)中添加下面几行:
"完全隐藏菜单:
:set guioptions-=m " 可以随时用 :set guioptions+=m 再呼出菜单,下面工具条也类似
"完全隐藏工具栏:
:set guioptions-=T

Thursday, December 20, 2007

Vim folding commands

zf#j creates a fold from the cursor down # lines.
zf/string creates a fold from the cursor to string .
zj moves the cursor to the next fold.
zk moves the cursor to the previous fold.
zo opens a fold at the cursor.
zO opens all folds at the cursor.
zm increases the foldlevel by one.
zM closes all open folds.
zr decreases the foldlevel by one.
zR decreases the foldlevel to zero -- all folds will be open.
zd deletes the fold at the cursor.
zE deletes all folds.
[z move to start of open fold.
]z move to end of open fold.

more usage command of vim folding can get from this page.

为gvim/vim增加tail -f功能

1.从http://code.google.com/p/vim-scripts/wiki/TailBundle上面Subversion导出文件
tail_options.vim
autoload/tail.vim
doc/tail.txt
macros/vim-tail.zsh
plugin/tail.vim
2.放到Gvim/vim对应目录下即可
3.使用方法
:Tail
:STail
:TabTail
4.可参考tail_options.vim设置~/_vimrc(~/.vimrc),也可不设置按默认的使用.

document referer and referrer

http request referer:
Referer http://www.google.cn/search?hl=zh-CN&q=%E6%B5%B7%E8%AF%8D&btnG=Google+%E6%90%9C%E7%B4%A2&meta=

php $_SERVER['http_referer']:
http://www.google.cn/search?hl=zh-CN&q=%E6%B5%B7%E8%AF%8D&btnG=Google+%E6%90%9C%E7%B4%A2&meta=

javascript referer:
document.referrer

Monday, December 17, 2007

vim substitute usage tips

统计文章中出现的单词的数目,可以使用下面的命令:
:%s/\w//gn

如何将一串十进制数字转换为16进制数字,使用VIM完成转换的最简单方法如下:
:%s/\d\+/\=printf("%X", submatch(0))/g
这条命令的原理是,把一串数字,用printf()函数的输出替换掉,printf()函数输出的正是这串数字的16进制形式。
分析如下:
\= 使用表达式的结果进行替换 (:help /\w )
printf 按指定格式输出 (:help printf() )
submatch() 返回:s命令中的指定匹配字符串 (:help submatch() )
g 替换行内所有出现的匹配 (:help :s_flags)

Friday, December 14, 2007

Rails validates_uniqueness_of(*attr_names) 说明


validates_uniqueness_of(*attr_names)
Validates whether the value of the specified attributes are unique across the system. Useful for making sure that only one user can be named "davidhh".

class Person < ActiveRecord::Base
validates_uniqueness_of :user_name, :scope => :account_id # 类似account_id, :user_name 组成表中的unique key
end
It can also validate whether the value of the specified attributes are unique based on multiple scope parameters. For example, making sure that a teacher can only be on the schedule once per semester for a particular class.

class TeacherSchedule < ActiveRecord::Base
validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id] # 类似3个字段组成表的unique key
end

Javascript document.write usage

var fragment = document.createDocumentFragment();
var testScript = document.createElement('script');
testScript.setAttribute('type', 'text/javascript');
testScript.setAttribute('src', 'http://www.test.com/test.php');// test.php 中有document.write('<iframe src....></iframe>')
fragment.appendChild(testScript);
document.body.appendChild(fragment);

程序在载入当前页和test.php后就不能再载入iframe里的scr指向的页面,并页面锁死,这个同在页面载完后调Function输出一个document.write一样的错误.

set gvim menu language

" gvim menu language
set langmenu=zh_CN.GBK
"set langmenu=en_US.GBK

Thursday, December 13, 2007

让gvim启动时窗口最大化

在用户目录下的~/_vimrc文件里加上此行(只是Windows生效)
au GUIEnter * simalt ~x "maximum the initial window

Thursday, December 06, 2007

Gvim中自动进行编码匹配和加载选定的配色方案

"自动进行编码匹配
set fileencodings=gb2312,ucs-bom,utf-8,chinese
"加载配色方案
colorscheme murphy

Wednesday, December 05, 2007

将post动作完成后redirect到另外get动作的页面避免页面刷新数据重复提交

如题,一个页面重定向的功能。

Ruby Faker install and usage

$ wget http://gems.rubyforge.org/gems/faker-0.2.0.gem
--11:33:09-- http://gems.rubyforge.org/gems/faker-0.2.0.gem
=> `faker-0.2.0.gem'
Resolving gems.rubyforge.org... 205.234.109.19
Connecting to gems.rubyforge.org|205.234.109.19|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://rubyforge.iasi.roedu.net/gems/faker-0.2.0.gem [following]
--11:33:10-- http://rubyforge.iasi.roedu.net/gems/faker-0.2.0.gem
=> `faker-0.2.0.gem'
Resolving rubyforge.iasi.roedu.net... 192.129.4.120
Connecting to rubyforge.iasi.roedu.net|192.129.4.120|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 43,520 (42K) [application/octet-stream]

100%[====================================>] 43,520 5.05K/s ETA 00:00

11:33:25 (5.04 KB/s) - `faker-0.2.0.gem' saved [43520/43520]

$ gem install faker
Successfully installed faker, version 0.2.0
Installing ri documentation for faker-0.2.0-...
Installing RDoc documentation for faker-0.2.0-...

$ irb
irb(main):002:0> require 'rubygems'
=> true
irb(main):003:0> require 'faker'
=> true
irb(main):005:0> Faker::Name.name
=> "Lauriane Stanton"
irb(main):006:0>
irb(main):007:0* Faker::Name.name
=> "Sallie Rippin"
irb(main):008:0> Faker::Name.name
=> "Selina Jerde"
irb(main):010:0> Faker::Internet.email
=> "elda.friesen@cassin.co.uk"
irb(main):011:0> Faker::Internet.email
=> "ervin@parkerblock.us"
irb(main):012:0> Faker::Internet.email
=> "jaron.bailey@abbotthintz.co.uk"
irb(main):013:0> Faker::Internet.email
=> "stan@torp.us"
irb(main):014:0> Faker::Internet.free_email
=> "yasmeen@yahoo.com"
irb(main):015:0> Faker::Internet.free_email
=> "ona@hotmail.com"
irb(main):016:0> Faker::Internet.free_email
=> "naomi@hotmail.com"
irb(main):017:0> Faker::Internet.free_email
=> "alisha@hotmail.com"
irb(main):018:0> Faker::Internet.user_name