Archive for January, 2007

海驾教练的博客

Monday, January 29th, 2007

周六下午练车,贴库。感觉不错,遭到表扬:你掌握的挺好

这个教练很有意思,练车的时候来了一个报名的学员,教练说你知道他怎么找到我的么我在新浪上建了一个博客-赶博客新潮的教练,他们就是通过博客联系我的

肃然起敬,这真是利用网络生活的典范:)

教练教的确实不错,大家可以通过他的博客报名

blog with ecto

Friday, January 26th, 2007

很早以前就从Robin Lu的blog上看到ecto,不过一直觉得在线写blog也没什么大不爽,毕竟之前DreamHost的速度还可以。但是地震之后就不行了,导致很长的一段时间都不能写,WP后台post那个页面打开至少半小时…

于是开始用ecto,要做的工作是hack一下xmlrpc.php,让ecto可以使用WP的UltimateTagsWarrior plugin,方式可以看Robin的两篇blog:

How to make ecto work with Ultimate Tag Warrior
How to make ecto work with Ultimate Tag Warrior - Part II

(这篇就是ecto写的,并且加了tag,测试一下)

另外发现了一个Google Earth针对ecto的插件,可以方便发geo blog,给个链接有兴趣的看看:

http://confluence.rave.ac.uk/confluence/display/SCIRC/Google+Earth+Geo-Blog+Snapshot+-+for+ecto+(Mac+OS+X)

Railsbench

Friday, January 12th, 2007

Install

#sudo gem install railsbench 

(install path on my mac: /opt/local/lib/ruby/gems/1.8/gems/railsbench-0.9.0)

 

Configuration

1. set $RAILS_ROOT

eg. on my mac, edit ~/.bash_profile, add a path as below:

export RAILS_ROOT=”/Users/suave/rubyapps/rails_app”

 

2. #railsbench base

   or

   #eval ‘railsbench path’

 

3. #sudo railsbench postinstall 

 

Prepare your application

1. #railsbench install

(copy benchmarks.rb and benckmarks.yml to your rails_app/config, copy benchmarking.rb to your rails_app/config/environments)

 

2. edit benchmarks.yml as what you want

 

Run railsbench

#railsbench perf_run 100 -bm=all

(”all” is a target specified in benchmarks.yml)

 

Environments

RAILS_ROOT

          must be set to point to your rails app

RAILS_PERF_DATA

          performance data sets will be stored into this directory
            if not set, $HOME will be used

RAILS_PERF_RUNS

          the number of times perf_loop will run perf_bench on a single invocation
            if not set, 3 runs will be performed

RAILS_BENCHMARK_FILE

          perf_bench send it’s output to this file

 

See http://railsbench.rubyforge.org/ or README under your railsbench install path to know more :-)

Ruby Performance Validator

Friday, January 12th, 2007

Ruby Performance Validator (RPVL) from Software Verification Ltd. (SVL)

You could check the product feature and  download a evaluated version from official website: http://www.softwareverify.com/ruby/profiler/index.html

SVL has lots of Ruby tools, eg. Ruby Bug Validator, Ruby Coverage Validator, Ruby Memory Validator and Ruby Thread Validator.

run rpvExample need Tk installed on your windows, so download Active Tcl (support various OS), type “require ‘tk’ “ in irb after installed Tcl/Tk, you could see “true”.

In RPVL, we could check CallStack, CallTree that are important to dissect the code.

Know more about RPVL, see the official tutorial that give you a introducion with a real example. At the last chapter, you could know how to use RPVL in Ruby on Rails.

 

Rails with Subversion (SVN)

Friday, January 12th, 2007

这篇文章总结自 Rails Wiki 的 HowtoUseRailsWithSubversion

 

首先是初始化 Rails app 的一系列命令,其中忽略了所有的 log 文件,tmp 文件,database.yml:

# svn import . repository_url -m “Import” –username user

# mv your_rails_app your_rails_app_backup

# svn checkout svn_url_to_your_repository your_rails_app

 

# svn remove log/*

# svn commit -m “removing all log files from svn”

# svn propset svn:ignore “*.log” log/

# svn update log/

# svn commit -m “ignore all file in /log/ ending .log”

 

# svn propset svn:ignore “*” tmp/sessions tmp/cache tmp/sockets

# svn commit -m “ignore all files in /tmp/”

 

# svn move config/database.yml config/database.yml.example

# svn commit -m “moving database.yml to database.yml.example to provide a template for anyone who checks out the code”

# svn propset svn:ignore “database.yml” config/

# svn update log/

# svn commit -m “ignore datase.yml”

如果需要忽略掉 tmp/ 下面的所有文件,可以这样:

# svn remove tmp/*

# svn propset svn:ignore “*” tmp/

# svn update tmp/

# svn commit -m “ignore tmp/ content from now”

如果 team 同时在 -nix 和 windows 上面开发,会有 shebang(#!) 不同的问题,可以忽略掉所有的 dispatch 文件:

# svn move public/dispatch.rb public/dispatch.rb.example

# svn move public/dispatch.cgi public/dispatch.cgi.example

# svn move public/dispatch.fcgi public/dispatch.fcgi.example

# svn commit -m “moving dispatch.* to dispatch.*.example to provide a template for anyone who checks out the code”

# svn propset svn:ignore “dispatch.*” public/

# svn update public/

# svn commit -m “ignore dispatch.* files”

对于 rails plugin 的管理,可以直接将 plugin 作为外部文件,当 update 的时候所有的 plugin 都会找自己的 svn trunk 去 update,我们也可以指定一个版本锁定我们当前的 plugin:

# ruby script/plugin install -x svn_url_to_plugin_repository

# svn commit -m “added plugin xxx”

# svn update

# svn propedit svn:externals vendor/plugins

 

globalize -r179 svn_url_to_plugin_repository 

 

开发过程中用 script/generate 产生的 rails 文件都需要手动添加到 svn 的工作拷贝中,如果觉得太麻烦可以使用下面的脚本,可以自动 svn add 所有的 status 为 ? 的文件:

 # svn status | grep “^\?” | awk “{print \$2}” | xargs svn add

如果文件名包含空格,可以用下面的脚本:

 # svn status | grep “^\?” | sed -e ’s/? *//’ | sed -e ’s/ /\ /g’ | xargs svn add

或者更省事的是在 bash 配置文件( 我mac上的配置文件是 ~/.bash_profile )里面加一个 alias :

alias svnaddall=’svn status | grep “^\?” | awk “{print \$2}” | xargs svn add’

编辑完记得 source ~/.bash_profile

some what advanced JavaScript

Wednesday, January 10th, 2007

这篇文章完全来自Sergio PereiraQuick guide to somewhat advanced JavaScript ,没有按照原文翻译,只是自己做了简单的注释。

 

用JSON的方式创建对象:

var myPet = {

          color: ‘black’,

          legCount: 4,

          communicate: function(repeatCount) {

                                  for( i = 0; i < reapeatCount; i++)

                                        alert('Woorf!');

                               }

};

 

alert('my pet is ' + myPet.color);

alert('my pet has ' + myPet.legCount + ' legs');

myPet.communicate(3);

在上面的代码中创建了一个对象的reference,对象包含两个属性(color和legCount)和一个方法(communicate)

 

在JS中,function也是一个对象,你可以将function作为另外一个function的参数,见下面的代码:

var myDog = {

             bark: function() {

                         alert(”Woorf!”);

             }

};

 

var myCat = {

             meow: function() {

                         alert(”I’m a lazy cat, I will not meow for you.”);

             }

};

 

function annoyThePet(petFunction) {

       petFunction();

}

 

annoyThePet(myDog.bark);

annoyThePet(myCat.meow);

注意,代码的最后两行我们将myDog.bark和myCat.meow传递给annoyThePet方法时没有加”()”,此外我们还可以这样做(我觉得是改变了对象的reference):

myCat.meow = myDog.bark;

myCat.meow(); //alerts “Wroof!”

JS里面对象(Object)和数组(Array)还有着莫名其妙的联系,下面两行用不同方式创建数组,效果一样:

var a = new Array();

var b = [];

对象的属性可以通过取数组元素的方式进行存取:

var obj = {}; // new empty object

obj['member_1'] = 1;

obj['flag_2'] = false;

obj['some_function'] = function(){ /* do something */};

调用的时候同样可以通过存取数组元素的方式:

obj.some_function();

obj['some_function']();

在JS中创建class也是比较诡异的事情,先看看怎么声明class的属性:

//define a class called Pet

var Pet = function(petName, age) {

      this.name = petName;

      this.age = age;

};

 

//let’s create an object of the Pet class

var famousDog = new Pet(”Santa\’s Little Helper’, 15);

alert(’This pet is called ” + famousDog.name); 

再说说怎么给class定义function(注意代码中的那个”this.name”的使用):

Pet.prototype.communicate = function() {

          alert(’I do not know what I should say, but my name is ‘ + this.name);

};

如果你使用prototype.js,创建对象的代码会更清晰:

var Pet = Class.create();

Pet.prototype = {

       initialize: function(petName, age) {

                this.name = petName;

                this.age = age;

       },

       communicate: function() {

                alert(’I do not know what I should say, but my name is ‘ + this.name);

       }

};

 

关于闭包(Closure):

var myArray = ['first', 'second', 'third'];

myArray.each( function(item, index) {

                        alert(’The item in the position #’ + index + ‘ is: ’ + item);

});

 

function myIterator(item, index) {

      alert(’The item in the position #’ + index + ‘ is: ’ + item);

}

var myArray = ['first', 'second', 'third'];

myArray.each( myIterator); 

最后有一个关于this关键字的问题:

var myHelper = {

          formFields: [],

          emptyAllFields: function() {

                      for( i = 0; i < this.formFields.length; i++) {

                            var elementID = this.formField[i];

                            var field = document.getElementById(elementID);

                            field.value = ”;

                      }

          }

};

 

myHelper.formFields.push(’txtName’);

myHelper.formFields.push(’txtEmail’);

myHelper.formFields.push(’txtAddress’);

 

var clearButton = document.getElementById(’btnClear’);

 

//clearButton.onclick = myHelper.emptyAllFields;

clearButton.onclick = function() { myHelper.emptyAllFields(); }; 

上面倒数第二行会导致一个runtime error,原因就在于emptyAllFields方法中的两个this。倒数第二行的代码将clearButton.onclick的reference直接指向了myHelper.emptyAllFields,这样emptyAllFields中的this表示的应该是clearButton,所以自然找不到formFields属性。