Monday, March 01, 2010

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

Wednesday, February 03, 2010

collapsing margins of table

display=block的元素,垂直方向的collapsing margins会发生重合。
IE8/safari/chrome中table默认的display=table,其渲染时vertical margin会发生重合,与display=block的元素效果一样,但在firefox中测试其效果与float元素的效果一样,不会发生垂直方向的margin重合。
个人的解决方法是将table设置为float=left,并在其后设置一个clear=both的DIV,避免table的margin被重合。

关于collapsing margins详细说明可查看CSS21规范的8.3.1 Collapsing margins。
另外对于一个maring:10px的DIV元素,如果此元素没有内容,并没有设置padding和border值,其上部的margin与其下部的margin也会重合,也就是此DIV在垂直方向上只会占据10px的高度。如果此DIV的top或者bottom与别的元素发生过重合,则其本身的top和bottom margin不会再重合。
规范原文说明如下:
If the top and bottom margins of a box are adjoining, then it is possible for margins to collapse through it. In this case, the position of the element depends on its relationship with the other elements whose margins are being collapsed.
If the element’s margins are collapsed with its parent’s top margin, the top border edge of the box is defined to be the same as the parent’s.
Otherwise, either the element’s parent is not taking part in the margin collapsing, or only the parent’s bottom margin is involved. The position of the element’s top border edge is the same as it would have been if the element had a non-zero top border.

Thursday, January 28, 2010

mysql 客户端中比较有用的几个命令

mysql> help

For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Network Support, training, or other products, visit:
   https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'

谈一下个人觉得上面比较有用的几个命令:
1、 edit
mysql>\e
输入\e后调用系统的默认文本编辑器,如vi出来,用于编辑SQL语句,避免直接在命令行中,太长的SQL输入错误,修改起来麻烦,输入完成保存退出即运行。

2、 pager
利用此命令最方便就是可以结合less查看输出结果,有时比用\G输出要方便整齐。在mysql命令行中输入:
mysql>pager less -S
之后的查询就会以不折行的形式输出到控制台。另一个用法是将SQL输出重定向到另外一个文本日志文件中:
mysql>pager cat > /tmp/sql_log.txt
以后的查询生成结果会被写入此文件中。

3、 tee
这个命令的功能与pager重定向日志文件类似,可以将生成的日志写入外部文件中。
mysql>tee /tmp/sql_log.txt

4、 system
在mysql console中运行外部操作系统的命令:
mysql>\! ls -l

5、 source
执行外部的SQL文件,有时在命令行中直接导入外部的一个SQL文件:
mysql>source /home/yu/test.sql;
这个与在操作系统命令行下执行:
$>mysql -u test -p < /home/yu/test.sql
效果一样。

Reference:
http://dev.mysql.com/doc/refman/5.0/en/mysql-commands.html