Friday, May 30, 2008

json for ruby [jruby] install and usage

$> sudo gem install json
$> sudo jruby -S gem install json_pure

require 'rubygems'
require 'json'

class Range
def to_json(*a)
{
'json_class' => self.class.name,
'data' => [ first, last, exclude_end? ]
}.to_json(*a)
end

def self.json_create(o)
new(*o['data'])
end
end

puts (1..10).to_json
p JSON.parse((1..10).to_json)
puts JSON.parse((1..10).to_json) == (1..10)

json =<<-"JSON"
{
"hasItems": true,
"totalItems": 5,
"orderId": "xxx-xxxxx-xxx",
"hasDetails": true,
"details": [{"item": "2323-2323", "number": 2, "price": 2.00}, {"item": "2323-2324", "number": 3, "price": 3.00}]
}
JSON
p JSON.parse(json)["hasItems"] # => true
p JSON.parse(json)["totalItems"] # => 5

notice: keys and values in json string must be quoted.

Wednesday, May 28, 2008

firefox addons for web developer

强力推荐 firefox addons: web developer/firefbug/HttpFox



2008下载日

Monday, May 26, 2008

标准的email正则表达式

"^\\w[-.\\w]*\\w@\\w[-.\\w]*\\w\\.\\w{2,6}$"

Sunday, May 25, 2008

MacBook 中 FIREFOX2 中文网页乱码及掉字问题解决方法

中文乱码主要原因是因为安装了OFFICE 2008对SIMSUN字体产生冲突,FIREFOX 3就没有这个问题。
FIREFOX 2下的解决方法就是在FONT BOOK中把SIMSUN这个字体先禁用掉。如果按下面修改了2个自定义的css文件,指定firefox的渲染字体,那么就不需要去禁用SIMSUN字体了。
可以打开http://www.163.com看一下是否正常。

在 firefox2 中还有一个就是在input框中输入的中文掉字和光标错位的问题,比如在google搜索框里输入"暂停删除"这四个字就可看得到。
这个需要修改一下 firefox2 的渲染css文件:
默认的目录是 ~/Library/Application Support/Firefox/Profiles/xxxxxxxx.default/chrome/
下面原来有2个example.css文件,对应的生成userChrome.css 和 userContent.css 这2个文件,并在这二个文件中都加入一个css定义:


* {
font-family: Lucida Grande, 华文黑体;
}

需要注意的是,一定要把文件保存为 UTF-8 格式。Lucida Grande 和华文黑体,都是苹果的默认字体,也是 Safari 默认字体。

Reference: http://www.wpdr.net/forums/showthread.php?t=3287
http://hi.baidu.com/dawgdash/blog/item/6157a138c501b227b8998ff1.html

Wednesday, May 21, 2008

install git on mac os leopard

# download asciidoc if os has no asciidoc.
# http://www.methods.co.nz/asciidoc/asciidoc-8.2.6.tar.gz
$ tar -xzf asciidoc-8.2.6.tar.gz
$ cd asciidoc-8.2.6
$ sudo ./install.sh
The uninstall.sh script (actually just a symlink to install.sh) will uninstall AsciiDoc.

# download git
# http://kernel.org/pub/software/scm/git/git-1.5.5.1.tar.bz2
$ make configure
$ ./configure --prefix=/usr
$ make
$ sudo make install

ruby script/server lighttpd

在一个jruby on rails目录下面运行以下命令时报错:
$> ruby script/server lighttpd
.....
Couldn't find any pid file in '/Users/yu/Sites/RubyOnRails/tmp/pids' matching 'dispatch.[0-9]*.pid'
(also looked for processes matching "/Users/yu/Sites/RubyOnRails/public/dispatch.fcgi")
在lighttpd的log中看到如下信息:
2008-05-21 00:23:33: (log.c.75) server started
2008-05-21 00:23:33: (mod_fastcgi.c.1029) the fastcgi-backend /Users/yu/Sites/RubyOnRails/public/dispatch.fcgi failed to start:
2008-05-21 00:23:33: (mod_fastcgi.c.1033) child exited with status 9 /Users/yu/Sites/RubyOnRails/public/dispatch.fcgi
2008-05-21 00:23:33: (mod_fastcgi.c.1036) If you're trying to run PHP as a FastCGI backend, make sure you're using the FastCGI-enabled version.
You can find out if it is the right one by executing 'php -v' and it should display '(cgi-fcgi)' in the output, NOT '(cgi)' NOR '(cli)'.
For more information, check http://trac.lighttpd.net/trac/wiki/Docs%3AModFastCGI#preparing-php-as-a-fastcgi-programIf this is PHP on Gentoo, add 'fastcgi' to the USE flags.
2008-05-21 00:23:33: (mod_fastcgi.c.1340) [ERROR]: spawning fcgi failed.
2008-05-21 00:23:33: (server.c.908) Configuration of plugins failed. Going down.

经google后才发觉script/server lighttpd只能用于ruby on rails的项目上,通过以下命令:
$> /usr/local/lighttpd/bin/spawn-fcgi -f /Users/yu/Sites/RubyOnRails/public/dispatch.fcgi -p 12000 -s /tmp/rails-fcgi.socket
会得到成功的结果如下:
spawn-fcgi.c.197: child spawned successfully: PID: 2275
而在jruby的项目下调用spawn-fcgi则会报脚本错误。

另附dispatch.sh[reference: http://www.javaeye.com/topic/168989]


#!/bin/sh

DISPATCH_PATH=/Users/yu/Sites/cv2/public/dispatch.fcgi
SOCKET_PATH=//Users/yu/Sites/cv2/tmp/sockets
RAILS_ENV=developement
export RAILS_ENV

case "$1" in

start)
for num in 0 1 2
do
/Users/yu/Programs/lighttpd/bin/spawn-fcgi -f $DISPATCH_PATH -s $SOCKET_PATH/rails.socket-$num
done
;;

stop)
killall -9 dispatch.fcgi
;;

restart)
$0 stop
$0 start
;;

*)
echo "Usage: dispatch.sh {start|stop|restart}"
;;

esac

exit 0

Tuesday, May 20, 2008

macbook and sudo

when I test below command in termial:
$> sudo ls -l .
then terminal will ask me input password, but current user has no password, and terminal will output nothing after entered.

after give current user a new password:
$> passwd
old password, new password, confirm new password, then type command in terminal again:
$> sudo ls -l .
input new password and enter, then results output as expected.

Wednesday, May 07, 2008

test scope of this in closure and in anonymous functions

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>test scope of this in closure and in anonymous functions</title>
<script type="text/javascript">
var foo = 'this is window.foo!';
var d = [1, 2, 3];
// timeout functions
function Constructor() {
this.foo = 'this is Constructor.foo!';
var that = this;
this.timerId = window.setTimeout(function() {
// alert(this); // will get [object Window]
alert("this.foo = " + this.foo);
alert("that.foo = " + that.foo);
} , 1000);
}
// local functions
Constructor.prototype.getFoo = function() {
alert(this); // [object Object]
var getExternalFoo = (function() {
// alert(this); // will get [object Window]
return d.concat(this.foo)
})();
return getExternalFoo;
};
// using Function.call(object)
Constructor.prototype.getBar = function() {
var getInternalFoo = (function() {
// alert(this); // will get [object Object]
return d.concat(this.foo)
}).call(this);
return getInternalFoo;
};
var f = new Constructor();
document.write(f.getFoo());
document.write("<br />");
document.write(f.getBar());

</script>
</head>
<body>
</body>
</html>

在window.setTimeout和全局环境中的匿名方法中的this是指向window对象的,所以在调用过程中可以将此匿名方法做为实际的某个对象(如this对象)的方法来调用.