Friday, March 26, 2010

notice "var" keywords of javascript in IE8

当前测试只是针对IE8,没有测试之前IE版本,以下代码在firefox/chrome中会按预期一样正常运行,代码测试注意打开firebug或者是console查看输出:


<script type="text/javascript">
// window.a 设置一个属性a到window上
window.a = 1;
// 正常打印出a:1
window.console.log(a);
</script>



<script type="text/javascript">
// 必须在此分二块script来写,如果写在一块script标签内,都会正常执行
// 在同一个script代码块内,javascript会解析当前代码块中所有的变量(之前没有出现过的变量名),并且都为 undefined
// 在这里还没有执行到var a=2,a为 undefined
// 针对IE8: window.a 也被置为 undefined
// 其他浏览器: window.a前面代码中已经定义,所以这里还是原来的值:1,而非像IE8中一样为 undefined
window.console.log(a);
var a = 2; // 注意前面有个变量声明: var
window.console.log(a);
</script>



<script type="text/javascript">
window.console.log(a);
// 这里因为没有用var新定义变量,所以a即为window.a,只是对变量重新赋值,代码执行没有问题
a = 3; // 注意前面没有变量声明: var
window.console.log(a);
</script>

以上代码在firefox/chrome中执行结果为: 1 1 2 2 3
以上代码在IE8中执行结果为: 1 undefined 2 2 3

隐藏ubuntu命令行中运行gvim的警告信息

$> gvim
** (gvim:6676): CRITICAL **: gtk_form_set_static_gravity: assertion `static_gravity_supported' failed
** (gvim:6676): CRITICAL **: gtk_form_set_static_gravity: assertion `static_gravity_supported' failed
** (gvim:6676): CRITICAL **: gtk_form_set_static_gravity: assertion `static_gravity_supported' failed
** (gvim:6676): CRITICAL **: gtk_form_set_static_gravity: assertion `static_gravity_supported' failed
** (gvim:6676): CRITICAL **: gtk_form_set_static_gravity: assertion `static_gravity_supported' failed

每次运行看到这些警告很不爽,网上找了一下,发现是一个待修正的问题,暂时只能将错误重定向到个人目录的.xsession-errors中,重写一个gvim的命令,放在个人主目录~/bin下:

#!/bin/bash
## Execute gvim, sending X/gtk errors to the standard place.
exec /usr/bin/gvim $* > /dev/null 2>>$HOME/.xsession-errors

Tuesday, March 09, 2010

一个tomcat启动二个不同端口的web服务

首先在server.xml模仿Service Catalina一样再配一个Service,修改Service/Engine的name,Connector的port

<Service name="Catalina2">
<Connector port="8081" />
<Engine name="Catalina2" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
</Host>
</Engine>
</Service>
其次复制conf下的Catalina整个目录为新的Service名字,注意里面localhost文件夹里的配置因项目不同,进行调整:
$> cp -r conf/Catalina conf/Catalina2

Monday, March 01, 2010

Ctags for rails and gvim

ctags支持41种语言,常见的 JAVA/C/C++/JavaScript/PHP/Perl/Python/Ruby等都包括其中,更多信息可查看官网帮助手册

在ubuntu中安装ctags:

$> sudo apt-get install exuberant-ctags


在rails项目下生成ctags文件,生成的TAGS文件可以用于vim/emacs等编辑器:
$> ctags-exuberant -a -e -f TAGS --tag-relative -R app lib vendor /opt/ruby-enterprise/lib/ruby/gems/1.8/gems


在gvim中常的操作有以下4个:
$> gvim −t tag

打开定义 tag 的文件。

在vi中使用:ta tag打开定义tag的文件
:ta tag


在vi的指针位置打开定义所在tag的文件
Ctrl-]


回退到之前的位置
Ctrl-T

Debugging rails with ruby-debug

1. setup
$> sudo gem install ruby-debug

2. start rails webrick/mongrel web server
$> script/server --debugger

3. using debugger in controller

class PeopleController < ApplicationController def new debugger # 当应用程序访问到此行时,在console中会出现debugger控制台 @person = Person.new end end

4. debugger shell 命令帮助
(rdb:40) help
ruby-debug help v0.10.3
Type 'help ' for help on a specific command

Available commands:
backtrace delete enable help next quit show
break disable eval info p reload source
catch display exit irb pp restart step
condition down finish list ps save thread
continue edit frame method putl set trace

其中比较常用的是next/list/continue/method/backtrace命令。

更多关于ruby-debug,查看以下链接:
http://articles.sitepoint.com/article/debug-rails-app-ruby-debug
http://guides.rubyonrails.org/debugging_rails_applications.html