rvm ruby 多版本控制

Posted by wxianfeng Mon, 22 Nov 2010 21:07:00 GMT

最终环境:ubuntu10.10 + ruby 1.8.7 + ruby 1.9.2 + rails 2.3.5 + rails 3.0.3

新的项目,使用最新的ruby1.9.2 + rails3.X开发,所以 环境必须重新搭建 ,但是又要能使之前的rails2.X项目跑起来,所以,就用到了rvm来控制,rvm确实不错,一直用部署的capistrano差不多,很方便,rvm的原理是动态的改变系统的path,即ruby的bin目录,来调用不同的ruby版本的,都有自己独立的文件夹,这样管理rails的那些gem也就独立开来了,核心是一个script,有兴趣的可以看下,$HOME/.rvm/scripts/rvm , 这个shell脚本,下面介绍安装过程:

需要用到 git 和 curl

sudo apt-get install git
sudo apt-get install curl

注意首先把原来的ruby bin目录从PATH中去掉

安装 rvm

cd ~
bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
cd .rvm/src/rvm
./install

查看 rvm 安装目录

wxianfeng@ubuntu:~/.rvm/gemsets/ruby$ echo $rvm_path
/home/wxianfeng/.rvm

~/.bashrc中添加下面代码(PATH之用) 很重要

if [[ -s "$HOME/.rvm/scripts/rvm" ]]  ; then source "$HOME/.rvm/scripts/rvm" ; fi  

安装 ruby 1.9.2

>rvm notes
sudo apt-get install build-essential bison openssl libreadline5 libreadline5-dev curl git zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev autoconf
rvm install 1.9.2

查看:

wxianfeng@ubuntu:~$ rvm list
rvm rubies
   ruby-1.9.2-p0 [ i386 ]

设置默认的 ruby 版本(必须设置)

>rvm --default ruby-1.9.2-p0

可以看到 相关信息

wxianfeng@ubuntu:~$ rvm info

ruby-1.9.2-p0:

  system:
    uname:       "Linux ubuntu 2.6.35-22-generic #33-Ubuntu SMP Sun Sep 19 20:34:50 UTC 2010 i686 GNU/Linux"
    bash:        "/bin/bash => GNU bash, version 4.1.5(1)-release (i686-pc-linux-gnu)"
    zsh:         " => not installed"

  rvm:
    version:      "rvm 1.1.0 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]"

  ruby:
    interpreter:  "ruby"
    version:      "1.9.2p0"
    date:         "2010-08-18"
    platform:     "i686-linux"
    patchlevel:   "2010-08-18 revision 29036"
    full_version: "ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]"

  homes:
    gem:          "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0"
    ruby:         "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0"

  binaries:
    ruby:         "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/ruby"
    irb:          "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/irb"
    gem:          "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/gem"
    rake:         "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0/bin/rake"

  environment:
    PATH:         "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0/bin:/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0@global/bin:/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin:/home/wxianfeng/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/system/mysql/bin:/usr/local/system/jdk1.6.0_22/bin"
    GEM_HOME:     "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0"
    GEM_PATH:     "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0:/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0@global"
    MY_RUBY_HOME: "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0"
    IRBRC:        "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/.irbrc"
    RUBYOPT:      ""
    gemset:       ""

这时发现你的PATH已经变了

wxianfeng@ubuntu:~$ echo $PATH
/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0/bin:/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0@global/bin:/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin:/home/wxianfeng/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/system/mysql/bin:/usr/local/system/jdk1.6.0_22/bin
wxianfeng@ubuntu:~$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]

ruby 1.9.2 安装成功

安装rails

wxianfeng@ubuntu:~$ gem install rails --pre --no-ri --no-rdoc
/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/gem:4: warning: Insecure world writable dir /usr/local/system in PATH, mode 040777
Successfully installed activesupport-3.0.0.rc2
Successfully installed builder-2.1.2
Successfully installed i18n-0.4.2
Successfully installed activemodel-3.0.0.rc2
Successfully installed rack-1.2.1
Successfully installed rack-test-0.5.6
Successfully installed rack-mount-0.6.13
Successfully installed tzinfo-0.3.23
Successfully installed abstract-1.0.0
Successfully installed erubis-2.6.6
Successfully installed actionpack-3.0.0.rc2
Successfully installed arel-1.0.0.rc1
Successfully installed activerecord-3.0.0.rc2
Successfully installed activeresource-3.0.0.rc2
Successfully installed mime-types-1.16
Successfully installed polyglot-0.3.1
Successfully installed treetop-1.4.9
Successfully installed mail-2.2.10
Successfully installed actionmailer-3.0.0.rc2
Successfully installed thor-0.14.6
Successfully installed railties-3.0.0.rc2
Successfully installed bundler-1.0.0.rc.6
Successfully installed rails-3.0.0.rc2
23 gems installed

有提示我path中有777权限目录,看着不顺眼,解决掉:

sudo chmod go-w /usr/local/system

发现安装上的是 3.0.0.rc2 不是我想要的,目前最新版是 3.0.3

咋办,用shell脚本全部remove掉

shell:

#!/bin/bash 

for i in `gem list`
do
        gem uninstall $i
done

最后发现rake无法uninstall掉,好那就进入目录把rake delete掉

wxianfeng@ubuntu:~$ gem list -d rake
/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/gem:4: warning: Insecure world writable dir /usr/local/system in PATH, mode 040777

*** LOCAL GEMS ***

rake (0.8.7)
    Author: Jim Weirich
    Rubyforge: http://rubyforge.org/projects/rake
    Homepage: http://rake.rubyforge.org
    Installed at: /home/wxianfeng/.rvm/gems/ruby-1.9.2-p0@global

    Ruby based make-like utility.


>cd /home/wxianfeng/.rvm/gems/ruby-1.9.2-p0@global

wxianfeng@ubuntu:~/.rvm/gems/ruby-1.9.2-p0@global$ ll
total 28
drwxr-xr-x 7 wxianfeng wxianfeng 4096 2010-11-23 14:14 ./
drwxr-xr-x 5 wxianfeng wxianfeng 4096 2010-11-23 14:14 ../
drwxr-xr-x 2 wxianfeng wxianfeng 4096 2010-11-23 15:21 bin/
drwxr-xr-x 2 wxianfeng wxianfeng 4096 2010-11-23 15:22 cache/
drwxr-xr-x 2 wxianfeng wxianfeng 4096 2010-11-23 15:22 doc/
drwxr-xr-x 2 wxianfeng wxianfeng 4096 2010-11-23 15:20 gems/
drwxr-xr-x 2 wxianfeng wxianfeng 4096 2010-11-23 15:21 specifications/

把bin cache doc gems specifications 下和rake相关的信息全部remove掉

重新安装成功 rails 3.0.3

wxianfeng@ubuntu:~$ gem install rails -v=3.0.3 --no-ri --no-rdoc
/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/gem:4: warning: Insecure world writable dir /usr/local/system in PATH, mode 040777
Successfully installed activesupport-3.0.3
Successfully installed builder-2.1.2
Successfully installed i18n-0.4.2
Successfully installed activemodel-3.0.3
Successfully installed rack-1.2.1
Successfully installed rack-test-0.5.6
Successfully installed rack-mount-0.6.13
Successfully installed tzinfo-0.3.23
Successfully installed abstract-1.0.0
Successfully installed erubis-2.6.6
Successfully installed actionpack-3.0.3
Successfully installed arel-2.0.4
Successfully installed activerecord-3.0.3
Successfully installed activeresource-3.0.3
Successfully installed mime-types-1.16
Successfully installed polyglot-0.3.1
Successfully installed treetop-1.4.9
Successfully installed mail-2.2.10
Successfully installed actionmailer-3.0.3
Successfully installed rake-0.8.7
Successfully installed thor-0.14.6
Successfully installed railties-3.0.3
Successfully installed bundler-1.0.7
Successfully installed rails-3.0.3
24 gems installed
wxianfeng@ubuntu:~$ rails -v
Rails 3.0.3

安装 ruby 1.8.7

>rvm install 1.8.7

切换到 1.8.7

wxianfeng@ubuntu:~/.rvm/gemsets/ruby$ rvm 1.8.7
wxianfeng@ubuntu:~/.rvm/gemsets/ruby$ gem list
/home/wxianfeng/.rvm/rubies/ruby-1.8.7-p302/bin/gem:4: warning: Insecure world writable dir /usr/local/system in PATH, mode 040777

*** LOCAL GEMS ***

rake (0.8.7)

悲剧的发现 其实 默认rake是安装上的,之前我还把它 给干掉 重新安装了

安装 rails 2.3.5

>gem install rails -v=2.3.5 --no-ri --no-rdoc

以后切换的话 就用 rvm —default 1.8.7 或者 rvm —default 1.9.2 切换默认的ruby版本,可以使用 rvm 1.8.7 切换当前终端版本到1.8.7 , 也就是可以同时开ruby 1.8.7 , ruby 1.9.2 的项目了

安装之前ruby 1.8.7 + rails 2.3.5 项目的 所有gems (注意把ruby版本切到1.8.7), 如果已经安装了,就不安装:

shell:

#!/bin/bash 
# 安装所有的gems,如果已经安装了就不安装

cd /usr/local/system/ruby/lib/ruby/gems/1.8/cache 


for i in `ls`;do
        gem=`echo $i | awk -F'-' '{print $1}'`
        version=`echo $i | grep -o "\-[0-9].*" | sed 's/^-//;s/.gem//'`
        is_gem_exist=`gem list $gem -v=$version`
        if [ -z "$is_gem_exist" ]; then # 注意[] 内部两边留空格
                `gem install $i`
        else
                echo "$i have installed"
        fi
done

这样,之前ruby 1.8.7+rails2.3.4的项目 和现在的 ruby 1.9.2 + rails 3.0.3 都兼容了…oh…YEAR

问题: netbeans 配置使用 ruby 1.8.7 + rails 2.3.5 OR ruby 1.9.2 + rails 3.0.3 ?
更改netbeans 的ruby path ,选择 autodetect 会把ruby的bin全部找出来,但是注意gem path 有可能错误,通过 rvm info 看下 gem 的位置,然后手动更改即可~!

SEE:

http://thoughtrails.com/episodes/1-installing-rails-3-on-ubuntu-10-04-lucid-lynx-with-rvm
http://rvm.beginrescueend.com/rvm/install/


最终环境 : ubuntu 10.10 + ruby 1.8.7 + rails 2.3.5 + mysql 5.1
换了新工作,重新搭建开发环境,鉴于之前mysql遇到问题,总是不好解决,束手无策,甚至找不到 mysql 安装哪去了,所以这次主要想编译安装mysql,这样有利于了解mysql

1,下载ruby1.8.7 (http://www.ruby-lang.org/en/news/2010/08/16/ruby-1-8-7-p302-is-released/)

wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p302.tar.bz2

2,安装依赖的库

sudo apt-get install build-essential 
sudo apt-get install autoconf 
sudo apt-get install zlib1g-dev 
sudo apt-get install openssl libssl-dev

3,编译安装ruby

tar -jxvf ruby-1.8.7-p302.tar.bz2 
cd ruby-1.8.7-p302/
autoconf
./configure --prefix=/usr/local/system/ruby
make
sudo make install

4,ruby 加到PATH 中

vim ~/.bashrc
export PATH="$PATH:/usr/local/system/ruby/bin"

5,ruby -v

安装rubygems(http://rubyforge.org/frs/?group_id=126&release_id=43601)

1,下载

wget http://rubyforge.org/frs/download.php/70696/rubygems-1.3.7.tgz

2,解压

tar -zvxf rubygems-1.3.7.tgz

3,安装

cd rubygems-1.3.7/
wxianfeng@ubuntu:~/Desktop/rubygems-1.3.7$ ruby setup.rb 
ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied - /usr/local/system/ruby/lib/ruby/site_ruby/1.8/rubygems
wxianfeng@ubuntu:~/Desktop/rubygems-1.3.7$ sudo ruby setup.rb 
[sudo] password for wxianfeng: 
sudo: ruby: command not found
改变权限
cd /usr/local
sudo chmod -R 777 system/ 
再次安装就ok了,不然你以后还会遇到这个问题

4,gem -v

安装rails 2.3.5

>gem install rails -v 2.3.5 --no-ri --no-rdoc

编译安装 mysql

1,下载并且编译安装
download (http://downloads.mysql.com/archives.php?p=mysql-5.1) 从archives 找到 ,注意下载的是source包

sudo apt-get install g++ libncurses5-dev # 安装依赖的库
sudo  groupadd mysql  # 建立mysql用户组
sudo  useradd -g mysql mysql # 添加用户mysql
tar -zvxf mysql-5.1.51.tar.gz 
cd mysql-5.1.51/
./configure --prefix=/usr/local/system/mysql --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=latin1(all) --with-plugins=innobase(all)  # config 很重要,括号里是或者
make
sudo make install
sudo cp support-files/my-medium.cnf /etc/my.cnf # 配置文件

注意mysql sock文件安默认装在tmp路径下 /tmp/mysql.sock,编译的时候 —with-plugins=all 建议加上 不然你会发现编译后不支持innodb引擎,那就麻烦了!

2, 初始化数据库并修改目录权限

cd /usr/local/system/mysql
sudo bin/mysql_install_db --user=mysql
sudo chown -R root .
sudo chown -R mysql /usr/local/system/mysql/var
sudo chgrp -R mysql .

3,启动mysql

bin/mysqld_safe --user=mysql &
netstat -antup | grep 3306
sudo cp /usr/local/system/mysql/share/mysql/mysql.server /etc/init.d/mysql
sudo killall mysqld # kill 刚刚启动的mysql
sudo /etc/init.d/mysql start|stop|restart|status # 重启mysql

4,添加PATH

>vim ~/.bashrc
export PATH="$PATH:/usr/local/system/mysql/bin"
wxianfeng@ubuntu:/usr/local/system/mysql/bin$ mysql --version
mysql  Ver 14.14 Distrib 5.1.51, for pc-linux-gnu (i686) using  EditLine wrapper

5, 修改mysql root 密码

>sudo /etc/init.d/mysql start --skip-grant-tables
>mysql -uroot -p mysql
>update user set password=passsword('root') where user='root'; 
重启mysql
就ok了 root 账户的密码为root

6,怎么判断我的mysql是编译安装的

wxianfeng@ubuntu:~$ mysql --version
mysql  Ver 14.14 Distrib 5.1.51, for pc-linux-gnu (i686) using  EditLine wrapper

如果你是apt-get 安装的话就显示 for ubuntu-linux-gnu
yum 安装的话就显示 for redhat-linux-gnu 了

遇到的问题:

1,
>mysql -uroot -p
Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

>mysql -uroot -p --socket=/tmp/mysql.sock 可以启动,所以是sock问题
后来发现 my.cnf 有两个配置文件,/etc/mysql/my.cnf 多了一个这个,里面sock路径指定不一样所致,rename 掉
>mv /etc/mysql/my.cnf /etc/mysql/my_cnf.bak

2,

Starting MySQL. * Manager of pid-file quit without updating file. 错误

wxianfeng@ubuntu:/usr/local/system/shell$ /etc/init.d/mysql start 
Starting MySQL. * Manager of pid-file quit without updating file.

需要加sudo

3,查看mysql data目录

root@ubuntu:/usr/local/system/mysql/var# ps auxf | grep mysql
root     25756  0.0  0.0   4012   764 pts/3    S+   15:32   0:00  |           \_ grep --color=auto mysql
root     25000  0.0  0.0   4904  1400 ?        S    14:22   0:00 /bin/sh /usr/local/system/mysql/bin/mysqld_safe --datadir=/usr/local/system/mysql/var --pid-file=/usr/local/system/mysql/var/ubuntu.pid
mysql    25106  0.0  0.8 119652 16568 ?        Sl   14:22   0:00  \_ /usr/local/system/mysql/libexec/mysqld --basedir=/usr/local/system/mysql --datadir=/usr/local/system/mysql/var --user=mysql --log-error=/usr/local/system/mysql/var/ubuntu.err --pid-file=/usr/local/system/mysql/var/ubuntu.pid --socket=/tmp/mysql.sock --port=3306

可以看出在var下 ,注意 var目录的用户是 mysql ,必须root用户才能cd进入

sudo su # 切换到root用户

4,启动rails project报错:因为mysql gem的版本过高的原因

uninitialized constant MysqlCompat::MysqlRes (NameError)
>sudo apt-get install libmysqlclient-dev
>gem install mysql -v=2.7 --no-ri --no-rdoc

SEE:
http://blog.sitearth.com/ubuntu%E4%B8%8A%E6%90%AD%E5%BB%BAlamp%E7%8E%AF%E5%A2%83%E7%AC%94%E8%AE%B0%E2%80%94%E2%80%94mysql%E6%BA%90%E7%A0%81%E5%AE%89%E8%A3%85%E5%8F%8A%E9%85%8D%E7%BD%AE/
http://51jsp.cn/html/ror/2010/0327/14859.html
see:http://qichunren.javaeye.com/blog/577556


ruby 源码学习 : h 方法

Posted by wxianfeng Thu, 18 Nov 2010 19:48:00 GMT

环境:ruby 1.8.7
我一直以为 h 是rails里的源码 ,其实是ruby里的erb.rb 中封装的
rails中,在页面输出的时候,h方法很常用 ,html , javascript ,ifream 都是源码输出,不会被浏览器执行了,其实现原理非常之简单,就是CGI.escapeHTML 的效果,代码都一样,一句正则的事情:

rails 中 h 使用方法:

<%= "<strong>HEY</strong>"%>
<%=h "<strong>HEY</strong>"%>

生成页面源码:

<strong>HEY</strong> <!-- 效果加粗输出 -->
&lt;strong&gt;HEY&lt;/strong&gt; <!-- 浏览器显示 : <strong>HEY</strong> -->

ruby 中 h 使用方法:

require 'erb'
p ERB::Util.h "<strong>HEY</strong>"

来看看 h 的源码:
/usr/local/system/ruby/lib/ruby/1.8/erb.rb

    def html_escape(s)
      s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
    end
    alias h html_escape

CGI.escapeHTML 源码:
/usr/local/system/ruby/lib/ruby/1.8/cgi.rb

  def CGI::escapeHTML(string)
    string.gsub(/&/n, '&amp;').gsub(/\"/n, '&quot;').gsub(/>/n, '&gt;').gsub(/</n, '&lt;')
  end

那么放过来怎么转呢,CGI.unescapeHTML即可

建议打开源码看下,还有常用的 url encode(escape) , decode(unescape) 等等的用法,让你不虚此行

简单的demo:

require 'rubygems'
require 'nokogiri'
require 'erb'
require 'cgi'

str = "<strong>HEY</strong>"
str1 = "&lt;strong&gt;HEY&lt;/strong&gt;"
url = "http://wxianfeng.com"

doc = Nokogiri::HTML str
doc1 = Nokogiri::HTML str1

p doc.inner_html # "<html><body><strong>HEY</strong></body></html>"
p doc1.inner_html # "<html><body><p>&lt;strong&gt;HEY&lt;/strong&gt;</p></body></html>"
p CGI.unescapeHTML(str1) #  "<strong>HEY</strong>"
p CGI.escapeHTML(str) # "&lt;strong&gt;HEY&lt;/strong&gt;"
p ERB::Util.h str # "&lt;strong&gt;HEY&lt;/strong&gt;"
p CGI.escape(url) # "http%3A%2F%2Fwxianfeng.com"
p CGI.unescape(CGI.escape(url)) # "http://wxianfeng.com"

shell 统计项目代码行数

Posted by wxianfeng Tue, 16 Nov 2010 17:52:00 GMT

你知道你目前的项目代码有多少行吗?有多少个文件吗?有多少个文件夹吗?。。。^o(∩∩)o…哈哈,这个很简单,一句shell就搞定了,效率还非常之高:

1,统计你项目的.rb 结尾的代码行数,去掉空格

find . -type f -name "*.rb" | xargs cat | grep -v ^$ | wc -l

当然你还可以添加去掉注释

2,统计项目文件所有行数,包括空行

find . -type f  | xargs cat | wc -l

3,统计项目有多少个文件

find . -type f  | wc -l

4,统计项目有多少个文件夹

find . -type d  | wc -l

5,找出项目下.rb 结尾的文件行数最大的一个(由小到大排序)

find . -type f -name "*.rb"  | xargs wc -l | sort -n

6,自由发挥

总之find 命令结合 xargs 很强大的说

这些都是基本的用法,例如上面想得到tar.gz 等压缩包的内容就难了,那么可以借助cloc(perl库)得到更详细的信息,各种文件类型的行数,注释,空行等等信息:

ubuntu 安装:

sudo apt-get install cloc

使用:

wxianfeng@ubuntu:/usr/local/system/projects/redmine_1_0_3$ cloc .
    6455 text files.
    3119 unique files.                                          
    1035 files ignored.

http://cloc.sourceforge.net v 1.09  T=13.0 s (204.8 files/s, 25293.4 lines/s)
-------------------------------------------------------------------------------
Language          files     blank   comment      code    scale   3rd gen. equiv
-------------------------------------------------------------------------------
Ruby               2076     33371     44800    178357 x   4.20 =      749099.40
YAML                197      1299       691     44512 x   0.90 =       40060.80
Javascript          105      1963      2111     12461 x   1.48 =       18442.28
Ruby HTML           241       716        29      4862 x   4.00 =       19448.00
CSS                  22       366       102      1889 x   1.00 =        1889.00
HTML                 15       120         3       627 x   1.90 =        1191.30
Perl                  2        80        98       233 x   4.00 =         932.00
SQL                   3        17         2        96 x   2.29 =         219.84
XML                   1         0         0         9 x   1.90 =          17.10
-------------------------------------------------------------------------------
SUM:               2662     37932     47836    243046 x   3.42 =      831299.72
-------------------------------------------------------------------------------

另外 rails 中已经封装了一个rake 任务,用来统计的,瞧瞧:

wxianfeng@ubuntu:/usr/local/system/projects/wxianfeng_com$ rake stats
(in /usr/local/system/projects/wxianfeng_com)
+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |  2422 |  1978 |      36 |     215 |   5 |     7 |
| Helpers              |   858 |   712 |       0 |     103 |   0 |     4 |
| Models               |  3360 |  2661 |      55 |     447 |   8 |     3 |
| Libraries            |  1203 |   884 |      30 |     143 |   4 |     4 |
| APIs                 |   436 |   353 |      17 |      23 |   1 |    13 |
| Functional tests     |    16 |    12 |       2 |       0 |   0 |     0 |
| Unit tests           |    16 |    12 |       3 |       0 |   0 |     0 |
| Model specs          |  2094 |  1680 |       1 |      24 |  24 |    68 |
| View specs           |   249 |   200 |       0 |      12 |   0 |    14 |
| Controller specs     |  3507 |  2734 |       2 |      37 |  18 |    71 |
| Helper specs         |    53 |    39 |       0 |       0 |   0 |     0 |
| Library specs        |    15 |    11 |       0 |       0 |   0 |     0 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                | 14229 | 11276 |     146 |    1004 |   6 |     9 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 6588     Test LOC: 4688     Code to Test Ratio: 1:0.7

see:

http://bbs.chinaunix.net/viewthread.php?tid=1665204
http://garfileo.is-programmer.com/2010/6/11/lines-counting-using-cloc.18828.html


正则 多行匹配

Posted by wxianfeng Mon, 15 Nov 2010 21:41:00 GMT

多行匹配也很常见,例如截取html源码的时候很有用,实现多行匹配只需要在正则后面加m即可 例如

/http:(.*?)\s?/m

ruby demo:

1

p "ab\r\n334cd".match(/ab(.*)cd/) # nil

p "ab\r\n334cd".match(/ab(.*)cd/m) # #<MatchData "ab\r\n334cd" 1:"\r\n334">

2,

str = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><pre>\n<code class=\"ruby\">\nputs \"hello world\"\nputs \"hello world\"\n</code>\n</pre></body></html>\n"

p str.match(/<body>(.*)<\/body>/m)[0] # "<body><pre>\n<code class=\"ruby\">\nputs \"hello world\"\nputs \"hello world\"\n</code>\n</pre></body>"

p str.match(/<body>(.*)<\/body>/)[0] # nil

rails + swfupload 文件上传 session 问题

Posted by wxianfeng Wed, 10 Nov 2010 13:43:00 GMT

环境:ruby 1.8.7 + rails 2.3.5 + swfupload

swfupload 上传文件很方便,支持多文件上传,但是在往往需要做知道这张图片是谁传的,这样就需要取当前登录的session,这样就会出现问题,

因为图片是通过flash传上来的,swfupload里的session和rails里的session不是同一个,理论上属于跨域问题了,所以rails取不到session值,所以必须解决session问题,网络上大家一般都是通过修改session来解决的,即吧session的参数从swfupload里给从传到后台,方可验证通过,eg: http://huacnlee.com/blog/rails-multi-files-upload-with-swfupload

我是通过关闭session,通过参数来验证的,例如传login名字,得到当前是哪个用户:

controller:

class CkeditorController < ApplicationController
skip_before_filter :verify_authenticity_token, :only => [:create]
session :off , :only => [:create]

def create
   @record.user ||= User.find_by_login(params[:login])
end

end

view 页面上:注意中文注释部分

<script>
window.onload = function() {
                upload1 = new SWFUpload({
                        // Backend Settings
                        upload_url: "/ckeditor/create",
                        post_params: {"login" : <%= current_user.login %>,"dir":dir}, // 这里很重要

                        // File Upload Settings
                        file_size_limit : "102400",        // 100MB
                        file_types : "*.*",
                        file_types_description : "All Files",
                        file_upload_limit : 100,
                        file_queue_limit : 0,

                        // Event Handler Settings (all my handlers are in the Handler.js file)
                        swfupload_preload_handler : preLoad,
                        swfupload_load_failed_handler : loadFailed,
                        file_dialog_start_handler : fileDialogStart,
                        file_queued_handler : fileQueued,
                        file_queue_error_handler : fileQueueError,
                        file_dialog_complete_handler : fileDialogComplete,
                        upload_start_handler : uploadStart,
                        upload_progress_handler : uploadProgress,
                        upload_error_handler : uploadError,
                        upload_success_handler : uploadSuccess,
                        upload_complete_handler : uploadComplete,

                        // Button Settings
                        button_image_url : "/images/XPButtonUploadText_61x22.png",
                        button_placeholder_id : "spanButtonPlaceholder1",
                        button_width: 61,
                        button_height: 22,

                        // Flash Settings
                        flash_url : "/javascripts/swfupload/swfupload.swf",
                        flash9_url : "/javascripts/swfupload/swfupload_fp9.swf",


                        custom_settings : {
                                progressTarget : "fsUploadProgress1",
                                cancelButtonId : "btnCancel1"
                        },

                        // Debug Settings
                        debug: false
                });

        }
</script>

英文环境下 netbeans 无法输入中文问题

Posted by wxianfeng Tue, 02 Nov 2010 22:23:00 GMT

环境:ubuntu 10.10 + ibus + netbeans 6.9.1

这个问题老早就遇到了,就是一直没解决,今天算是找到一个完美的办法了,用过fcitx ,scim ,ibus 输入法,都会有同样的问题,以前遇到的解决办法

1,重启netbeans
2,重启输入法

完美解决办法:

在桌面上新建一个shell文件,然后拖到你的panel上,每次启动netbeans的时候,点击panel上的这个shell文件就ok了

相当于 终端上执行了 >LANG=zh_CN.UTF-8 /bin/sh “/usr/local/system/netbeans-6.9.1/bin/netbeans” —locale en_US

>vim nb.sh
#!/bin/sh
LANG=zh_CN.UTF-8 /bin/sh "/usr/local/system/netbeans-6.9.1/bin/netbeans" --locale en_US 

>chmod +x nb.sh

or

>vim nb.sh
#!/bin/sh
/bin/sh "/usr/local/system/netbeans6_9/bin/netbeans" --locale zh_CN.UTF-8

>chmod +x nb.sh

终端 daemon 执行:

nohup  /bin/sh "/usr/local/system/netbeans6_9/bin/netbeans" --locale zh_CN.UTF-8  &

see:
http://www.linuxsir.org/bbs/thread370506.html