<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
  <channel>
    <title>路旁一棵葱</title>
    <description></description>
    <link>http://jonny131.javaeye.com</link>
    <language>UTF-8</language>
    <copyright>Copyright 2003-2008, JavaEye.com</copyright>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>JavaEye - 做最棒的软件开发交流社区</generator>
          <item>
        <title>Ruby + Apache+ mod_fcigd 配置笔记</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/245474" style="color:red;">http://jonny131.javaeye.com/blog/245474</a>&nbsp;
          发表时间: 2008年09月22日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 因为公司不想用别的WEB服务器，所以只能把Ruby源码发布到现有的Apache上，弄了半天终于搞定，留个笔记，供大家参考。<br />
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 安装环境&nbsp;&nbsp; Linux&nbsp; CentOs5, Apache 2.2.9, Ruby 1.86, Rails 2.1.0.&nbsp; gem 1.2.0<br />
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 使用的模块是 ： fast cgi ,&nbsp;&nbsp; mod_fcgid.<br />
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为什么使用mod_fcgid 请看&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href=" http://fastcgi.coremail.cn/index.htm" target="_blank"> http://fastcgi.coremail.cn/index.htm</a>
<br />
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1. 下载所需文件源码<br />
<br /></p>
<pre name="code" class="java">         wget  http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz



          wget  http://nchc.dl.sourceforge.net/sourceforge/mod-fcgid/mod_fcgid.2.2.tgz</pre>
<p>
<br />
<br />
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2. 编译安装 fcgi<br />
<br /></p>
<pre name="code" class="java">        tar -zxvf  fcgi-2.4.0.tar.gz

           cd fcgi-2.4.0

           ./configure

           make

           make install</pre>
&nbsp;
<p>
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3. 编译安装 mod_fcgid模块<br />
<br />
<br /></p>
<pre name="code" class="java">tar -zxvf mod_fcgid.2.2.tgz

cd mod_fcgid.2.2

 注意：如果 apache 没有安装在 /usr/local/apache2 修改 mod_fcgid.2.2文件夹里的
 top_dir 为现在apache的路径，同时这里还需要apache  src和build路径,建议都放到一起，这样改动比较少，
也不容易出错.


make

make install
</pre>
&nbsp;
<p>
&nbsp;&nbsp;&nbsp; 完成后没有错误的话 mod_fcgid.so 文件会自动拷贝到&nbsp; apache/ modules/&nbsp; 目录下.<br />
<br />
&nbsp;&nbsp; 4. 安装 fcgi gem</p>
<p>&nbsp;</p>
<pre name="code" class="java">gem install fcgi
</pre>
<p>&nbsp;</p>
<p>


&nbsp;&nbsp;&nbsp; 如果错误提示：can&rsquo;t find header files for ruby ,&nbsp; 需要安装 ruby-devel 包. <br />
<br />
&nbsp; 5. apache 配置<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在 httpd.conf 里添加下面的代码</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<pre name="code" class="java">LoadModule fcgid_module modules/mod_fcgid.so
           SocketPath /tmp/fcgidsock
           DefaultInitEnv RAILS_ENV production


         &lt;Directory /htdocs/railsproject/public&gt;
               Options ExecCGI FollowSymLinks
               AllowOverride AuthConfig Indexes Limit
               Order allow,deny
               Allow from all

               AddHandler fcgid-script .fcgi


              # You need mod_fcgid version &gt;= 2.1 to support arguments &quot;xxx/dispatch.fcgi&quot; in FCGIWrapper

              FCGIWrapper &quot;/htdocs/railsproject/public/dispatch.fcgi&quot; .fcgi

               RewriteEngine On
               RewriteRule ^$ index.html [QSA]
               RewriteRule ^([^.]+)$ $1.html [QSA]
               RewriteCond %{REQUEST_FILENAME} !-f
               RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
           &lt;/Directory&gt;
</pre>
&nbsp;
<p>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ok ，重启apache 后就可以在&nbsp; http://yourdomain/railsproject/public/ 里访问到了，这样的url 虽然很不好看，但目的已经达到了，需要的同学可以自己加vhost.<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 参考资料:<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HowtoSetupApacheWithFastCGIAndRubyBindings<br />
<br />
<br />
<br />
<br />
<br /></p>
<p>&nbsp;</p>
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/245474#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 22 Sep 2008 16:12:18 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/245474</link>
        <guid>http://jonny131.javaeye.com/blog/245474</guid>
      </item>
          <item>
        <title>ActionController::InvalidAuthenticityToken异常 解决办法</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/209816" style="color:red;">http://jonny131.javaeye.com/blog/209816</a>&nbsp;
          发表时间: 2008年06月30日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>ActionController::InvalidAuthenticityToken 异常一般出现出现在手写的html form 标签和Ajax请求中，为什么会出现这个异常呢，这是从rails 2.0 开始包含的一个新功能，目的在于防止CSRF(Cross-Site Request Forgery)攻击.</p>
<p>&nbsp;</p>
<p> 
     rails 为了保证当前的请求是来自自己的请求，而不是通过其他网站伪造的请求，都会在生成的form 里加入一个隐藏的值
</p>
<pre name="code" class="html">&lt;input type=&quot;hidden&quot; 
value=&quot;457a1e93c3a23ec2c67f1dc468bde6a4a5539610&quot; 
name=&quot;authenticity_token&quot;/&gt;</pre>
&nbsp;
<p>生成的ajax代码:</p>
<pre name="code" class="js">onsubmit=&quot;new Ajax.Updater('div', '/cotroller1/action1/1', 
{asynchronous:true, evalScripts:true, parameters:Form.serialize(this) 
+ '&amp;authenticity_token=' 
+ encodeURIComponent('457a1e93c3a23ec2c67f1dc468bde6a4a5539610')})</pre>
<p>&nbsp;</p>
<p>从上面的代码里可以看出关键是要向action 传递authenticity_token这个参数和正确的值</p>
<p>当然如果你认为你的action确实不需要验证，那可以这么写:</p>
<p>&nbsp;</p>
<pre name="code" class="js">#除了index,controller里的其他action都需要验证
protect_from_forgery :except =&gt; :index

#只有index需要验证
protect_from_forgery :only =&gt; :index</pre>
&nbsp;
<p>&nbsp;</p>
<p>如果是在ajax里可以在服务端生成好需要传入的字符串</p>
<p>&nbsp;</p>
<pre name="code" class="ruby">&quot;'#{request_forgery_protection_token}='
+ encodeURIComponent('#{escape_javascript 
 form_authenticity_token}')&quot;</pre>
&nbsp;
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/209816#comments" style="color:red;">已有 <strong>3</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 30 Jun 2008 16:01:17 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/209816</link>
        <guid>http://jonny131.javaeye.com/blog/209816</guid>
      </item>
          <item>
        <title>线性同余算法，伪随机数</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/188829" style="color:red;">http://jonny131.javaeye.com/blog/188829</a>&nbsp;
          发表时间: 2008年05月02日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>线性同余法求伪随机数的公式：<br />
X(n+1) = (a Xn + c) mod m<br />
<br />
公式中 0 &lt; m , 0&lt;=a&lt;m, 0&lt;c&lt;m,&nbsp; 0&lt;=X(0)&lt;m<br />
<br />
公式主要用到递归方法得到模除后的伪随机数,从X(0)到X(n),每一项都只依赖于前面一项，X序列将产生m个不同的数，然后重复。</p>
<p>&nbsp;</p>
<pre name="code" class="c">int rand(unsigned int seed)
{
   return (unsigned int)(seed * 1103515245 + 12345) % 32768;
}



int main()
{
     int x=12,i=0;

     for(i;i&lt;100;i++)
     {
        x = rand(x);
        printf(&quot;%d,&quot;,x);   
     }
     
     return 0;
}</pre>
&nbsp;
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/188829#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Fri, 02 May 2008 13:43:46 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/188829</link>
        <guid>http://jonny131.javaeye.com/blog/188829</guid>
      </item>
          <item>
        <title>读《programming ruby》笔记 四 Blocks Iterators</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/179842" style="color:red;">http://jonny131.javaeye.com/blog/179842</a>&nbsp;
          发表时间: 2008年04月06日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          这两个月比较忙，没有时间更新BLOG，今天开始继续。<br />ruby 中的block 和iterator 为我们在迭代编程中提供了更简单和自然的方法,block 跟随在方法调用后面，以{}包含代码部分。方法内部用yield调用block中的代码内容，block 的作用简单的说就是影响方法体内的代码行为，类似的功能比如在C#里的delegate,c/c++里的方法指针。<br /><pre name="code" class="ruby">def three_times
  i=1
  while i&lt;4
    yield i
    i +=1;
  end  
  
end

three_times{ |i| puts i}

#output
#1
#2
#3
</pre><br /><br />有了block，在数组的循环应用中就可以写出十分简洁的语句<br /><br />打印数组元素中的第一个字符,这里用到了数组的each方法。<br /><pre name="code" class="ruby">
%w{hello world ruby}.each { |str| puts str[0,1] }
#output
#h
#w
#r
</pre><br /><br />计算int数组的和,这里用到了inject方法,sum是用来得到执行的值，等价于sum = sum+item,<br />最后返回sum。<br /><pre name="code" class="ruby">
puts [1,3,5,7,9].inject{ |sum,item| sum+item }
#output
#25
</pre><br /><br /><br />下面再看看一个Block的实际应用<br />在Rails中layout是做为模板页使用的，可有的时候我们需要在不同的页面调用不同的JS,又不想全部写在模板页里面，这个时候可以使用block来灵活调用.<br /><br />在ApplicationHelper module里定义pagejs方法<br /><pre name="code" class="ruby">
def pagejs(*jsname)
    jsname.each{|item| content_for("pagejs"){ javascript_include_tag(item) }}
end
</pre><br /><br />然后在layout/index.rhtml 里加入<br /><pre name="code" class="ruby">&lt;%= yield :pagejs %></pre><br /><br />最后在要使用的view页面的第一行调用<br /><pre name="code" class="ruby">&lt;%pagejs("user")%></pre><br /><br /><br />呈现出的html源码中就会出现<br />&lt;script src="/javascripts/user.js?1207145282" type="text/javascript">&lt;/script>
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/179842#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sun, 06 Apr 2008 15:09:27 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/179842</link>
        <guid>http://jonny131.javaeye.com/blog/179842</guid>
      </item>
          <item>
        <title>读《programming ruby》笔记 三 Class Objects Variables</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/160075" style="color:red;">http://jonny131.javaeye.com/blog/160075</a>&nbsp;
          发表时间: 2008年01月29日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          &nbsp;&nbsp;&nbsp; 定义一个类用 Class ，类名一般以大写开头。每个类都有一个特殊的方法 initialize 这个是类的初始化函数，等同于Java 或C#面的构造函数。<br /><pre name="code" class="ruby">class Book
  attr_accessor :title,:content
  def initialize(title,content)
    @title = title
    @content = content
  end
end

book = Book.new(&quot;world is flat&quot;,&quot;world ...&quot;)
puts book.title
puts book.content

#output:
#world is flat
#world ...</pre><p>&nbsp;</p><p>在Ruby中类是永远不会被关闭的，这意味着我们可以在任何时候添加或重写类的方法，当然也可以是变量.</p><p>&nbsp;</p><pre name="code" class="ruby">class Book
  attr_accessor :author
end
book.author = &quot;danny&quot;
puts book.author

# output
# danny</pre><p>代码接上的例子，如果我在这里再次定义Book类里的内容，实际上是新增了一个 author的 attribute。</p><p>&nbsp;</p><p> 类的继承使用 &lt; 号，子类继承父类的方法和属性，这里我们设计一个卡通书CartoonBook，它将继承它的父类的initialize方法,并且加一个新属性是否彩页。</p><pre name="code" class="ruby">class CartoonBook &lt; Book
  attr_accessor :iscolor
end

book = CartoonBook.new(&quot;dargon ball&quot;, &quot;all image......&quot;)
book.iscolor = true

puts book.title
puts book.iscolor

# output:
# dargon ball
# true
</pre>&nbsp;<p>变量的类型有实例变量：Instance Variables , 类变量：Class&nbsp; Variables,变量:Variables.全局变量:Global Variables.第一个例子中我们就用到了instance variable,@title,同时在第一个例子中我们还用到了一个方法attr_accessor,这个方法用来向外部暴露我们的一个实例变量,也就是定义这个类的属性,attr_accessor定义为读写,attr_reader为只读,attr_writer为只写。</p><p> &nbsp;&nbsp; 类变量在这个类的所有实例中是共享的，ruby中使用@@符号来定义一个类变量。类变量必须先被初始化才能使用，初始化的的工作通常就是在类的定义里为类变量赋值。</p><pre name="code" class="ruby">class CartoonBook &lt; Book
  attr_accessor :iscolor
  @@total_rent =0
  def initialize(title,content)
    super
  end 
  
  def rent_to(stutdent)
    @@total_rent += 1 
    puts &quot;total rented #@@total_rent books&quot;
  end
end

book1 = CartoonBook.new(&quot;dargon ball&quot;, &quot;all image......&quot;)
book1.rent_to(&quot;davi&quot;)

book2 = CartoonBook.new(&quot;micky mouse&quot;,&quot;all image......&quot;)
book2.rent_to(&quot;marry&quot;)

#output:
# total rented 1 books
# total rented 2 books
</pre>&nbsp;上面的例子扩展了CartoonBook这个类,定义了@@total_rent类变量并赋值0,新增了rent_to方法,该方法实现把一本书借个一个学生同时打印书一个借出去了多少卡通书。book1,boo2都是CartoonBook类的实例,book1.rent_to后打印&quot;total rented 1 books&quot;说明@@total_rent==1,当book2.rent_t后@@total_rent==2,这说明了实例变量@@total_rent是在CartoonBook类中的所有实例中是共享一份copy的。<br /><p>&nbsp;</p><p> &nbsp;&nbsp;  变量:Variables指的是一般变量，它表示对对象的引用，比如上面的book1,book2就是变量，它们表示对CartoonBook对象的引用。name=&quot;jack&quot;中name是对String类型对象的引用。</p><p>&nbsp;</p><p> 全局变量:Global Variables是在整个程序里都有效的变量。$DEBUG表示现在是否是调试模式，__FILE__当前源文件名，$LOAD_PATH当前环境变量，除了这些还有很多，在书的650页有详细的介绍。</p><p>&nbsp;</p><p>&nbsp;</p><p> 常量:Constants,常量全部用大写字母，在Class和Module中定义的常量调用 Class::CONST,在外面定义的常量</p><p>直接调用或 ::CONST.</p><p>&nbsp;</p>
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/160075#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Tue, 29 Jan 2008 11:14:46 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/160075</link>
        <guid>http://jonny131.javaeye.com/blog/160075</guid>
      </item>
          <item>
        <title>最近眼睛疼的厉害</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/159261" style="color:red;">http://jonny131.javaeye.com/blog/159261</a>&nbsp;
          发表时间: 2008年01月25日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          干这行的什么少条腿都没什么，眼睛疼头疼还真要命，根本没有办法工作啊。<br />&nbsp;&nbsp;&nbsp; 我的近视就200度左右，上班的时候不想戴眼镜，就把显示屏拉的很近，现在一个星期眼睛要疼一次，都是到了下午的时候，眼睛疼头也有点疼不知道怎么回事。这个星期我天天下午眼睛都不舒服，一下班出去就好点了，看来我还是要戴上眼镜，把屏幕放远一点的好，眼镜时间戴长了也很不舒服的，嗨～～～。
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/159261#comments" style="color:red;">已有 <strong>26</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Fri, 25 Jan 2008 10:35:48 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/159261</link>
        <guid>http://jonny131.javaeye.com/blog/159261</guid>
      </item>
          <item>
        <title>读《programming ruby》笔记 二  Numbers Ranges</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/158781" style="color:red;">http://jonny131.javaeye.com/blog/158781</a>&nbsp;
          发表时间: 2008年01月23日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p> Ruby 支持Integer和浮点型数字类型，在Ruby中Integer 可以达到任何长度，只要你的机器还有可用的内存。Integer 分为 Fixnum 和 Bignum两种类型,Fixnum 范围在 -2^30 到2^30-1,比这更大的范围是Bignum。</p><pre name="code" class="ruby">puts (2**30).class
puts (2**30-1).class

#output:
#Fixnum
#Bignum</pre><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;&nbsp; 当一个Fixnum长度变大到Bignum的长度时对象会自动变换自己的类为Bignum,相反当Bignum计算结果属于Fixnum的范围时也会变换自己的类为Fixnum。</p><p> &nbsp;&nbsp; 下面看看和Integer的一些常用技巧</p><p>&nbsp;</p><pre name="code" class="ruby">3.times{|i| puts i.to_s}
#output:
# 0
# 1
# 2</pre><p>&nbsp; 上面的例子使用times方法，表示执行3次{}里面的代码,也可以：</p><pre name="code" class="ruby">3.times{ puts &quot;times&quot;}

#output:
# times
# times
# times</pre><p>&nbsp;</p><p> &nbsp;&nbsp;  upto 和 downto</p><p> &nbsp;&nbsp;&nbsp; </p><pre name="code" class="ruby">3.downto(0) { |i| puts i }
#output
# 3
# 2
# 1
# 0</pre><p>&nbsp;</p><p> &nbsp;&nbsp;  </p><p>&nbsp;</p><p> &nbsp;&nbsp;  &lt;=&gt; 和===，&lt;=&gt;是比较两个数字，比如a&lt;=&gt;b，如果a&lt;b返回-1，等于返回0，大于返回1。===表示属于关系比如：</p><p>&nbsp;</p><p>&nbsp;</p><p> &nbsp;&nbsp;  </p><pre name="code" class="ruby">puts (1..5) === 3
puts (1..5) === 9

#output:
# true
# false</pre><p>&nbsp;&nbsp; 上面的代码说明了 3属于1到5之间，而9是不属于1到5之间的，这里不光是整数类型,3.5也是属于1..5的：</p><p>&nbsp;</p><pre name="code" class="ruby">puts (1..5) === 3.5

#output:
# true
</pre><p>&nbsp;</p><p>&nbsp;</p><p> 1..5在Ruby中是一个Range类型对象。Range 当然不只是数字，字符串、日期等等都可以是Range:</p><p>&nbsp; </p><pre name="code" class="ruby">puts ('a'..'e').to_a

#output
# a
# b
# c
# d
# e
</pre><p>&nbsp;&nbsp; </p><p> &nbsp;&nbsp; 1..5表示1到5并包含5，1...5 不包含5. Range 用在for 循环中</p><p>&nbsp;&nbsp;&nbsp; </p><pre name="code" class="ruby">for i in 1...5
  puts i
end

#output:
# 1
# 2
# 3
# 4</pre>&nbsp;<p> &nbsp;&nbsp;  (1..5) === 3也可以这样写</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><pre name="code" class="ruby">range = 1..5
puts range.include?(3)

#output:
# true</pre><p>&nbsp; </p><p> 使用Range的step方法打印1到10中的单数,下面表示每隔两个数字（包含第二个数字）打印一次</p><p>&nbsp;</p><p>&nbsp; &nbsp;</p><p>&nbsp;</p><p> &nbsp;&nbsp;  </p><pre name="code" class="ruby">range = 1..10
range.step(2){|i| puts i}

#output:
# 1
# 3
# 5
# 7
# 9</pre><p>&nbsp;</p><p>下面让我们新建一个WeekDays来看看如何定义一个支持Range的类</p><pre name="code" class="ruby">class WeekDays
  DAYNAME ={'monday'=&gt;1,'tuesday'=&gt;2,'wednesday'=&gt;3,
'thursday'=&gt;4,'friday'=&gt;5,'saturday'=&gt;6,'sunday'=&gt;7}
  attr_accessor :day
  
  
  def initialize(day) 
    @day = day.downcase
  end
  
  # Support for ranges
  def &lt;=&gt;(other)
    DAYNAME[@day] &lt;=&gt; DAYNAME[other.day]
  end
  
  def succ
    raise(IndexError, &quot;Value is wrong&quot;) unless DAYNAME.has_key?(@day)
    WeekDays.new(DAYNAME.index(DAYNAME[@day].succ))
  end
end

 for  weekday in WeekDays.new('Monday')..WeekDays.new('Sunday')
   puts weekday.day
 end


#output:
# monday
# tuesday
# wednesday
# thrusday
# friday
# saturday
# sunday
</pre>&nbsp;<p>&nbsp;</p>
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/158781#comments" style="color:red;">已有 <strong>2</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Wed, 23 Jan 2008 16:39:55 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/158781</link>
        <guid>http://jonny131.javaeye.com/blog/158781</guid>
      </item>
          <item>
        <title>读《programming ruby》笔记 一 String </title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/153956" style="color:red;">http://jonny131.javaeye.com/blog/153956</a>&nbsp;
          发表时间: 2008年01月22日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p> 学习了一段时间的Ruby on Rails,用它做了一些小东西，开始是直接看的《Agile Web Development with Rails 2nd》虽然很多东西做出来了，但是依然有些不懂的地方，所以现在回过头来好好看看Ruby的书。</p><p> 在ruby中所有的事物都是对象，String也不例外，含有两个同样值的字符串其实是两个object,</p><pre name="code" class="ruby">a = &quot;string&quot;
b = &quot;string&quot;
c = a

puts a.object_id
puts b.object_id
puts c.object_id
#输出：
#
#21676710
#21676690
#21676710</pre><p>&nbsp;</p><p><span style="font-weight: bold">字符串的连接</span>：</p><pre name="code" class="ruby">a =&quot;hello,&quot;
b =&quot;world!&quot;
puts a+b
puts a&lt;&lt;b
#输出：
#hello,world!
#hello,world!</pre><p>如上面的代码示例，可以用 &quot;+&quot;和&quot;&lt;&lt;&quot;进行两个字符串的连接。如果字符串太长在代码里需要换行可以用下面的方法：</p><pre name="code" class="ruby">a =&quot;hello,\
       world!&quot;
b =&quot;hello,&quot;\
  +&quot;world!&quot;
puts a
puts b
#输出：
#hello,       world!
#hello,world!</pre><p>&nbsp;上面我们演示了在双引号内和引号外的两种换行连接方式，在这里需要注意，第一种方法输出的结果中&quot;world&quot;字符前面带有空格，这是因为在给a赋值时world前有空格，而第二行的格式是包含在引号内的。</p><p>&nbsp;</p><p>双引号和单引号的区别：</p><p>双引号内支持更多的转义字符 比如\n</p><pre name="code" class="ruby">a =&quot;hello,\nworld!&quot;
b ='hello,\nworld!'
puts a
puts b
#输出：
#hello,
#world!
#hello,\nworld!</pre><p>双引号内还支持表达式</p><p>&nbsp;</p><pre name="code" class="ruby">a = &quot;jack&quot;
puts &quot;who is #{a}&quot;

#输出：
#who is jack</pre><p>&nbsp;表达式以#{expression}的形式插入双引号内输出表达式的值。</p><p>全局变量和类变量不需要{}，如下：</p><pre name="code" class="ruby">$greeting = &quot;Hello&quot; # $greeting is a global variable
@name = &quot;Prudence&quot; # @name is an instance variable
puts &quot;#$greeting, #@name&quot;

#输出：
#Hello, Prudence</pre>&nbsp;<p>正则表达式:</p><p> ruby支持perl 标准的正则表达式，使用符号=~</p><pre name="code" class="ruby">a = &quot;php is programming language ,ruby too&quot;
puts a if a=~/php.*ruby/ 
puts a=~/php ruby/ 

#输出：
#php is programming language ,ruby too
#nil
</pre><p>&nbsp;如果匹配成功则返回匹配的字符在字符串中的位置。</p><p>在ruby中还有一些全局变量可以方便的使用，$`返回匹配串前面的字符，$&amp;返回匹配的字符串，$'返回匹配串后面的字符串。</p><p>&nbsp;</p><pre name="code" class="ruby">def show_regexp(a, re)
  if a =~ re
    &quot;#{$`}&lt;&lt;#{$&amp;}&gt;&gt;#{$'}&quot;
  else
    &quot;no match&quot;
  end
end

puts show_regexp('Fats Waller', /a/)
#输出：
#F&lt;&lt;a&gt;&gt;ts Waller</pre>&nbsp;<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> 
<p>&nbsp;</p><p>&nbsp;</p>
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/153956#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Tue, 22 Jan 2008 10:34:46 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/153956</link>
        <guid>http://jonny131.javaeye.com/blog/153956</guid>
      </item>
          <item>
        <title>java.lang.NullPointerException 错误</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/145108" style="color:red;">http://jonny131.javaeye.com/blog/145108</a>&nbsp;
          发表时间: 2007年11月30日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          如果你的andriod 开发后一段时间发现每次启动后都会报java.lang.NullPointerException错误，模拟器中的很多程序不能使用，那就是这个错误了：<br />
Linux: ~/.android/userdata.img <br />
Windows: C:\Documents and Settings/&lt;loginname&gt;/Local Settings/Application Data/Android/userdata.img<br />
<br />
删掉userdata.img文件就好了
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/145108#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Fri, 30 Nov 2007 15:13:15 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/145108</link>
        <guid>http://jonny131.javaeye.com/blog/145108</guid>
      </item>
          <item>
        <title>Rake 简介</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/138787" style="color:red;">http://jonny131.javaeye.com/blog/138787</a>&nbsp;
          发表时间: 2007年11月07日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>作者：<a href="http://www.railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial">gregg</a><br />
翻译：<a href="http://blog.absolute2.cn/posts/39"><strong>andy</strong></a></p>
<p>作为一个Rails开发者，你可能熟练得运用&ldquo;rake&rdquo;运行你的测试，或者你利用&ldquo;rake db：migrate&rdquo;运行你的数据迁移任务（migration）。但是，你是否真正明白在那些Rake任务的背后发生了什么吗？你是否意识到，你可以 编写属于你的任务，或者创建好用的属于你的Rake库文件？<span id="more-39"></span></p>
<p>下面是我运用Rake任务的一些例子：</p>
<ul>
    <li>创建一个成员列表，并给他们发送电子邮件。</li>
    <li>进行每天的数据计算和报告。</li>
    <li>清除过期和重新创建缓存。</li>
    <li>数据库备份和subversion repository。</li>
    <li>运行任何类型的数据操作脚本。</li>
    <li>为即将开始我们的谈话倒酒（玩笑）</li>
</ul>
<p>在这篇文章中，我们将理解为什么Rake被发明，以及它如何协助我们的Rails应用程序。最终你将能够编写属于你的任务。</p>
<h2>内容列表</h2>
<ol>
    <li>回顾：为什么是make？</li>
    <li>如何获取Rake？</li>
    <li>Rake如何工作？</li>
    <li>如何表达rake依赖？</li>
    <li>如何为Rake任务写文档？</li>
    <li>Rake命名空间</li>
    <li>如何写有用的Ruby任务？</li>
    <li>如何为我的Rails应用写Rake任务？</li>
    <li>可以在任务中访问Rails模型（Model）吗？</li>
    <li>哪里可以找到更多的例子？</li>
</ol>
<h2>回顾：为什么是make？<a name="why"></a></h2>
<p>为了理解我们为什么拥有Rake，我们首先需要注目一下Rake的祖父Make。<br />
和我回忆一下过去，在解释性语言和iPhones诞生之前，每一段代码都需要编译。</p>
<p>那时候如果你下载大型程序，它们总是伴随着大量源代码和一个shell 脚本。 这个脚本包含你的计算机为了编译/链接/创建应用程序，需要的每一行代码。你必须运行&ldquo;install_me.sh&rdquo;(shell脚本)，每一行代码将运 行（编译每一个源文件），然后编译出可执行文件。</p>
<p>对大多数人来说，此过程工作正常，除非你是开发程序的所有人中极其不幸运的一个。每一次你对源代码做了很小改动，并且想测试它，你必须重新运行shell脚本来重新编译所有代码。显然，如果是大型程序这将消耗大量时间。</p>
<ol>
    <li>Make 识别那些文件/资源在最后一次编译被改动。利用这个信息，Make在第二次执行的时候，<strong>只编译被改动过的源文件</strong>。这动态地减少了重新编译大型程序的时间。</li>
    <li>Make也包含<strong>依赖跟踪（dependency tracking）</strong>，所以你可以告诉编译器，源文件A需要源文件B来正确编译，源文件B依赖源文件C来正确编译。因此，如果Make编译源文件A而源文件B尚未编译,那么，系统将首先编译源文件B。</li>
</ol>
<p>也可以这样解释，<strong>&ldquo;Make&rdquo;是一个简单的可执行程序</strong>，像&ldquo;dir&rdquo;或&ldquo;ls&rdquo;一样。为了让Make如何知道编译一个程序，必须创建一个&ldquo;makefile&rdquo;文件，它描述所有的源文件和依赖关系。</p>
<p>经过多年的发展，其它编程语言甚至开始运用Make。事实上，许多Ruby编程人员在rake出来之前一直运用它。<br />
<strong> &ldquo;但是，Ruby不需要被编译，所以为什么Ruby程序员使用它？&rdquo;</strong>你如此叫道。</p>
<p>是的，Ruby是一个解释性的语言，我们不需要编译我们的代码，那么为什么Ruby程序员使用Make文件呢？</p>
<p>好吧，有两个主要原因：</p>
<ol>
    <li><strong>创建任务</strong>&mdash;在每一个大型程序最后，你都要编写命令行下可以运行的脚本。或许你想清除缓存，运行一个维护任务， 或者迁移数据库。除吧创建10个单独的shell脚本（或者一个大型的复杂脚本），你可以创建单个&ldquo;Makefile&rdquo;文件，在其中你可以通过任务组织所 有的东西。然后任务可以通过键入&ldquo;make stupid&rdquo;来运行。</li>
    <li><strong>依赖任务跟踪</strong>&mdash;当你开始编写维护任务的库的时候，你开始注意到某些任务可能部分重复。例如，任务 &ldquo;migrate&rdquo;和任务&ldquo;schema:dump&rdquo;都需要连接一个数据库。我可以创建叫&ldquo;connect_to_database&rdquo;一个任务，让任务 &ldquo;migrate&rdquo;和任务&ldquo;schema:dump&rdquo;都依赖于&ldquo;connect_to_database&rdquo;下次我运行&ldquo;migrate&rdquo;，在 &ldquo;migrate&rdquo;运行之前&ldquo;connect_to_database&rdquo;将被运行。</li>
</ol>
<h2>如何获取Rake？<a name="how"></a></h2>
<p>不少年以前<a href="http://onestepback.org/">Jim Weirich</a> 工作在一个使用Make的Java项目。当使用他的Makefile，他意识到，在他的Makefile中，如果他能够编写小的Ruby代码片段，事情会 变得异常方便。所以他创造了rake。上个月在Railsconf会议，我们幸运的见到了Jim，他的确是个不错的人。</p>
<p>Jim加入了创建任务功能，进行任务依赖跟踪，甚至加入同样时间戳识别。（只构建最后一次编译被改动的源文件）。显然，最后一个特性不是我们在Ruby中经常用到的。因为我们不需要编译。<br />
我一直惊奇&ldquo;Jim Weirich&rdquo;所做的一切，现在你也知道了！Jim从来没有特意的编写Make，我猜想他只是不得以写出来的。</p>
<h2>Rake如何工作？<a name="how_does_rake_work"></a></h2>
<p>起初，我想给这部分加个&ldquo;如何因Rake变得挥霍&rdquo;的标题，但是，不是很直觉得那么做。</p>
<p>假设我想喝醉，需要那几个步骤？</p>
<ol>
    <li>买酒</li>
    <li>喝酒</li>
    <li>喝醉</li>
</ol>
<p>如果我想运用Rake来调用以上的任务，可以创建一个包含以下内容的&ldquo;Rakefile&rdquo;文件：</p>
<div class="wp_syntax">
<table>
    <tbody>
        <tr>
            <td class="line_numbers">
            <pre>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></pre>
            </td>
            <td class="code">
            <pre class="ruby">task <span style="color: rgb(255, 51, 51); font-weight: bold;">:purchaseAlcohol</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />  <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;Purchased Vodka&quot;</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br />&nbsp;<br />task <span style="color: rgb(255, 51, 51); font-weight: bold;">:mixDrink</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />  <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;Mixed Fuzzy Navel&quot;</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br />&nbsp;<br />task <span style="color: rgb(255, 51, 51); font-weight: bold;">:getSmashed</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />  <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;Dood, everthing's blurry, can I halff noth'r drinnnk?&quot;</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span></pre>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p>然后我在rake文件的同一个目录下运行每一个任务，有一点如下所示：</p>
<div class="wp_syntax">
<div class="code">
<pre class="text">$ rake purchaseAlcohol<br /> Purchased Vodka<br />&nbsp;<br />$ rake mixDrink<br /> Mixed Fuzzy Navel<br />&nbsp;<br />$ rake getSmashed<br /> Dood, everthing's blurry, can I halff noth'r drinnnk?</pre>
</div>
</div>
<p>相当酷！不管怎样，从依赖的立场，我可以任何顺序运行任务。虽然有时候，我希望在&ldquo;mixDrink&rdquo;或者&ldquo;purchaseAlcohol&rdquo;之前，我可以&ldquo;getSmashed&rdquo;，这不是简单人力所能及。</p>
<h2>如何表达rake依赖？<a name="dependancies"></a></h2>
<div class="wp_syntax">
<table>
    <tbody>
        <tr>
            <td class="line_numbers">
            <pre>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></pre>
            </td>
            <td class="code">
            <pre class="ruby">task <span style="color: rgb(255, 51, 51); font-weight: bold;">:purchaseAlcohol</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />  <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;Purchased Vodka&quot;</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br />&nbsp;<br />task <span style="color: rgb(255, 51, 51); font-weight: bold;">:mixDrink</span> =&gt; <span style="color: rgb(255, 51, 51); font-weight: bold;">:purchaseAlcohol</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />  <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;Mixed Fuzzy Navel&quot;</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br />&nbsp;<br />task <span style="color: rgb(255, 51, 51); font-weight: bold;">:getSmashed</span> =&gt; <span style="color: rgb(255, 51, 51); font-weight: bold;">:mixDrink</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />  <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;Dood, everthing's blurry, can I halff noth'r drinnnk?&quot;</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span></pre>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p>所以，现在我说&ldquo;为了mixDrink，我必须先purchaseAlcohol&rdquo;，&ldquo;为了getSmashed 我必须mixDrink&rdquo;。也许如你希望，依赖堆叠，因此：</p>
<div class="wp_syntax">
<div class="code">
<pre class="text">$ rake purchaseAlcohol<br /> Purchased Vodka<br />&nbsp;<br />$ rake mixDrink        <br /> Purchased Vodka<br /> Mixed Fuzzy Navel<br />&nbsp;<br />$ rake getSmashed<br /> Purchased Vodka<br /> Mixed Fuzzy Navel<br /> Dood, everthing's blurry, can I halff noth'r drinnnk?</pre>
</div>
</div>
<p>正如你所看到的，现在当我将要&ldquo;getSmashed&rdquo;，依赖的任务&ldquo;purchaseAlcohol&rdquo;和&ldquo;mixDrink&rdquo;被调用。</p>
<p>经过不久，你可能被诱惑不断膨胀你的酒瘾，因此而扩充你的Rakefile文件。你也许会是你的朋友染上酒瘾。正像一个真正的软件项目，当你的团队增加成员的时候，你需要保证你有良好的文档。问题成了：</p>
<h2>如何为rake任务写文档？ <a name="documentation"></a></h2>
<p>Rake提供了一个简洁的方法来为task编写文档，下面是用法示例：</p>
<div class="wp_syntax">
<table>
    <tbody>
        <tr>
            <td class="line_numbers">
            <pre>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></pre>
            </td>
            <td class="code">
            <pre class="ruby">desc <span style="color: rgb(153, 102, 0);">&quot;This task will purchase your Vodka&quot;</span><br />task <span style="color: rgb(255, 51, 51); font-weight: bold;">:purchaseAlcohol</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />  <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;Purchased Vodka&quot;</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br />&nbsp;<br />desc <span style="color: rgb(153, 102, 0);">&quot;This task will mix a good cocktail&quot;</span><br />task <span style="color: rgb(255, 51, 51); font-weight: bold;">:mixDrink</span> =&gt; <span style="color: rgb(255, 51, 51); font-weight: bold;">:purchaseAlcohol</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />  <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;Mixed Fuzzy Navel&quot;</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br />&nbsp;<br />desc <span style="color: rgb(153, 102, 0);">&quot;This task will drink one too many&quot;</span><br />task <span style="color: rgb(255, 51, 51); font-weight: bold;">:getSmashed</span> =&gt; <span style="color: rgb(255, 51, 51); font-weight: bold;">:mixDrink</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />  <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;Dood, everthing's blurry, can I halff noth'r drinnnk?&quot;</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span></pre>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p>正如你所看到的，每一个任务有一个&ldquo;desc&rdquo;。这允许我们和朋友键入&ldquo;rake -T&rdquo;或者&ldquo;rake &ndash;tasks&rdquo;</p>
<div class="wp_syntax">
<div class="code">
<pre class="text">$rake --tasks<br /> rake getSmashed        # This task will drink one too many<br /> rake mixDrink          # This task will mix a good cocktail<br /> rake purchaseAlcohol   # This task will purchase your Vodka</pre>
</div>
</div>
<p>很简单吧？</p>
<h2>Rake命名空间<a name="namespaces"></a></h2>
<p>一旦你成为一个酒徒，并且使用大量的Rake任务，你可能需要一个好一点的办法对他们加以分类。这是为什么使用命名空间的原因。如果我在上面的例子使用命名空间，它看起来将是这样：</p>
<div class="wp_syntax">
<table>
    <tbody>
        <tr>
            <td class="line_numbers">
            <pre>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></pre>
            </td>
            <td class="code">
            <pre class="ruby">namespace <span style="color: rgb(255, 51, 51); font-weight: bold;">:alcoholic</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />  desc <span style="color: rgb(153, 102, 0);">&quot;This task will purchase your Vodka&quot;</span><br />  task <span style="color: rgb(255, 51, 51); font-weight: bold;">:purchaseAlcohol</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />    <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;Purchased Vodka&quot;</span><br />  <span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br />&nbsp;<br />  desc <span style="color: rgb(153, 102, 0);">&quot;This task will mix a good cocktail&quot;</span><br />  task <span style="color: rgb(255, 51, 51); font-weight: bold;">:mixDrink</span> =&gt; <span style="color: rgb(255, 51, 51); font-weight: bold;">:purchaseAlcohol</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />    <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;Mixed Fuzzy Navel&quot;</span><br />  <span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br />&nbsp;<br />  desc <span style="color: rgb(153, 102, 0);">&quot;This task will drink one too many&quot;</span><br />  task <span style="color: rgb(255, 51, 51); font-weight: bold;">:getSmashed</span> =&gt; <span style="color: rgb(255, 51, 51); font-weight: bold;">:mixDrink</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />    <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;Dood, everthing's blurry, can I halff noth'r drinnnk?&quot;</span><br />  <span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span></pre>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p>命名空间允许你根据分类为任务分组，是的，你可以在一个Rakefile中拥有多个命名空间。现在，如果我执行&ldquo;rake &ndash;tasks&rdquo;，我将看到：</p>
<div class="wp_syntax">
<div class="code">
<pre class="text">rake alcoholic:getSmashed        # This task will drink one too many<br />rake alcoholic:mixDrink          # This task will mix a good cocktail<br />rake alcoholic:purchaseAlcohol   # This task will purchase your Vodka</pre>
</div>
</div>
<p>所以，现在运行以上任务，显然你将运行&ldquo;rake alcoholic:getSmashed&rdquo;</p>
<h2>如何写有用的Ruby任务？<a name="do_stuff"></a></h2>
<p>好了，你尽管写ruby代码就可以了。不是开玩笑。最近，我需要一个创建几个目录的脚本，所以我最终编写了一个Rake任务，如下：</p>
<div class="wp_syntax">
<table>
    <tbody>
        <tr>
            <td class="line_numbers">
            <pre>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></pre>
            </td>
            <td class="code">
            <pre class="ruby">desc <span style="color: rgb(153, 102, 0);">&quot;Create blank directories if they don't already exist&quot;</span><br />task<span style="color: rgb(0, 102, 0); font-weight: bold;">(</span><span style="color: rgb(255, 51, 51); font-weight: bold;">:create_directories</span><span style="color: rgb(0, 102, 0); font-weight: bold;">)</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />&nbsp;<br />  <span style="color: rgb(0, 128, 0); font-style: italic;"># The folders I need to create</span><br />  shared_folders = <span style="color: rgb(0, 102, 0); font-weight: bold;">[</span><span style="color: rgb(153, 102, 0);">&quot;icons&quot;</span>,<span style="color: rgb(153, 102, 0);">&quot;images&quot;</span>,<span style="color: rgb(153, 102, 0);">&quot;groups&quot;</span><span style="color: rgb(0, 102, 0); font-weight: bold;">]</span><br />&nbsp;<br />  <span style="color: rgb(153, 102, 204); font-weight: bold;">for</span> folder <span style="color: rgb(153, 102, 204); font-weight: bold;">in</span> shared_folders<br />&nbsp;<br />    <span style="color: rgb(0, 128, 0); font-style: italic;"># Check to see if it exists</span><br />    <span style="color: rgb(153, 102, 204); font-weight: bold;">if</span> <span style="color: rgb(204, 0, 255); font-weight: bold;">File</span>.<span style="color: rgb(153, 0, 204);">exists</span>?<span style="color: rgb(0, 102, 0); font-weight: bold;">(</span>folder<span style="color: rgb(0, 102, 0); font-weight: bold;">)</span><br />      <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;#{folder} exists&quot;</span><br />    <span style="color: rgb(153, 102, 204); font-weight: bold;">else</span><br />      <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;#{folder} doesn't exist so we're creating&quot;</span><br />      <span style="color: rgb(204, 0, 255); font-weight: bold;">Dir</span>.<span style="color: rgb(153, 0, 204);">mkdir</span> <span style="color: rgb(153, 102, 0);">&quot;#{folder}&quot;</span><br />    <span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br />&nbsp;<br />  <span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span></pre>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p>默认情况下，rake可以访问<a href="http://corelib.rubyonrails.org/classes/FileUtils.html">File Utils</a>里的任何东西，但是你可以包含任何额外的Ruby中的任何东西。</p>
<h2>如何为我的Rails应用写Rake任务？<a name="rails_rake"></a></h2>
<p>Rails应用程序附带着许多预先存在的rake任务，你可以进入应用程序目录下键入&ldquo;rake &ndash;tasks&rdquo;来显示这些任务。如果你还没有尝试，马上尝试，我会等待&hellip;.</p>
<p>为你的Rails应用创建新的rake任务，你需要打开/lib/tasks目录（目录必须已经存在）。在这个目录中，如果你创建属于你的 Rakefile，并且命名为&ldquo;something.rake&rdquo;，任务将自动被接受。任务将被增加到应用程序rake任务的列表中，你可以从应用程序的根 目录运行他们。让我们吧上面的例子，应用到我们的rails应用程序。</p>
<p><small><strong>utils.tasks</strong></small></p>
<div class="wp_syntax">
<table>
    <tbody>
        <tr>
            <td class="line_numbers">
            <pre>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></pre>
            </td>
            <td class="code">
            <pre class="ruby">namespace <span style="color: rgb(255, 51, 51); font-weight: bold;">:utils</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />  desc <span style="color: rgb(153, 102, 0);">&quot;Create blank directories if they don't already exist&quot;</span><br />  task<span style="color: rgb(0, 102, 0); font-weight: bold;">(</span><span style="color: rgb(255, 51, 51); font-weight: bold;">:create_directories</span><span style="color: rgb(0, 102, 0); font-weight: bold;">)</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />&nbsp;<br />    <span style="color: rgb(0, 128, 0); font-style: italic;"># The folders I need to create</span><br />    shared_folders = <span style="color: rgb(0, 102, 0); font-weight: bold;">[</span><span style="color: rgb(153, 102, 0);">&quot;icons&quot;</span>,<span style="color: rgb(153, 102, 0);">&quot;images&quot;</span>,<span style="color: rgb(153, 102, 0);">&quot;groups&quot;</span><span style="color: rgb(0, 102, 0); font-weight: bold;">]</span><br />&nbsp;<br />    <span style="color: rgb(153, 102, 204); font-weight: bold;">for</span> folder <span style="color: rgb(153, 102, 204); font-weight: bold;">in</span> shared_folders<br />&nbsp;<br />      <span style="color: rgb(0, 128, 0); font-style: italic;"># Check to see if it exists</span><br />      <span style="color: rgb(153, 102, 204); font-weight: bold;">if</span> <span style="color: rgb(204, 0, 255); font-weight: bold;">File</span>.<span style="color: rgb(153, 0, 204);">exists</span>?<span style="color: rgb(0, 102, 0); font-weight: bold;">(</span><span style="color: rgb(153, 102, 0);">&quot;#{RAILS_ROOT}/public/#{folder}&quot;</span><span style="color: rgb(0, 102, 0); font-weight: bold;">)</span><br />        <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;#{RAILS_ROOT}/public/#{folder} exists&quot;</span><br />      <span style="color: rgb(153, 102, 204); font-weight: bold;">else</span><br />        <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;#{RAILS_ROOT}/public/#{folder} doesn't exist so we're creating&quot;</span><br />        <span style="color: rgb(204, 0, 255); font-weight: bold;">Dir</span>.<span style="color: rgb(153, 0, 204);">mkdir</span> <span style="color: rgb(153, 102, 0);">&quot;#{RAILS_ROOT}/public/#{folder}&quot;</span><br />      <span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br />&nbsp;<br />    <span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br />  <span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span></pre>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p>注意，在代码片断中，我是如何使用#{RAILS_ROOT}来得到全路径的。如果我现在我应用程序的根目录下，运行&ldquo;rake &ndash;tasks&rdquo;，我将看到这个新功能将和其它rails rake任务混合在一起。</p>
<div class="wp_syntax">
<table>
    <tbody>
        <tr>
            <td class="line_numbers">
            <pre>1<br />2<br />3<br />4<br />5<br />6<br /></pre>
            </td>
            <td class="code">
            <pre class="text">...<br />rake tmp:pids:clear              # Clears all files in tmp/pids<br />rake tmp:sessions:clear          # Clears all files in tmp/sessions<br />rake tmp:sockets:clear           # Clears all files in tmp/sockets<br />rake utils:create_directories    # Create blank directories if they don't already exist<br />...</pre>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p>很酷！现在，此处它变得比较有用。</p>
<h2>可以在任务中访问Rails模型（Model）吗？<a name="rails_models"></a></h2>
<p><strong>当然！</strong>事实上这正是我们使用rake的主要用途：<strong>编写偶尔需要手动运行的任务，或者定时调度以自动运行的任务（使用<a href="http://www.aota.net/Script_Installation_Tips/cronhelp.php3">cronjobs</a>）</strong>。正如我在文章开始说的，我使用Rake任务来做下面的事情：</p>
<ul>
    <li>创建一个成员列表，并给他们发送电子邮件。</li>
    <li>进行每天的数据计算和报告。</li>
    <li>清除过期和重新创建缓存。</li>
    <li>数据库备份和subversion repository。</li>
    <li>运行任何类型的数据操作脚本。</li>
</ul>
<p>非常有用，而且简单。下面是一个查找订购关系即将过期用户、并发送电子邮件的的rake任务。</p>
<p><small><strong>utils.tasks</strong></small></p>
<div class="wp_syntax">
<table>
    <tbody>
        <tr>
            <td class="line_numbers">
            <pre>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></pre>
            </td>
            <td class="code">
            <pre class="ruby">namespace <span style="color: rgb(255, 51, 51); font-weight: bold;">:utils</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />  desc <span style="color: rgb(153, 102, 0);">&quot;Finds soon to expire subscriptions and emails users&quot;</span><br />  task<span style="color: rgb(0, 102, 0); font-weight: bold;">(</span><span style="color: rgb(255, 51, 51); font-weight: bold;">:send_expire_soon_emails</span> =&gt; <span style="color: rgb(255, 51, 51); font-weight: bold;">:environment</span><span style="color: rgb(0, 102, 0); font-weight: bold;">)</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />        <span style="color: rgb(0, 128, 0); font-style: italic;"># Find users to email</span><br />        <span style="color: rgb(153, 102, 204); font-weight: bold;">for</span> user <span style="color: rgb(153, 102, 204); font-weight: bold;">in</span> User.<span style="color: rgb(153, 0, 204);">members_soon_to_expire</span><br />            <span style="color: rgb(204, 0, 102); font-weight: bold;">puts</span> <span style="color: rgb(153, 102, 0);">&quot;Emailing #{user.name}&quot;</span><br />            UserNotifier.<span style="color: rgb(153, 0, 204);">deliver_expire_soon_notification</span><span style="color: rgb(0, 102, 0); font-weight: bold;">(</span>user<span style="color: rgb(0, 102, 0); font-weight: bold;">)</span><br />        <span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br />  <span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span></pre>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p>正如你看到的，只有一个步骤访问你的模型（model），&ldquo;=&gt; :environment&rdquo;：</p>
<ol>
    <li>task(:send_expire_soon_emails =&gt; :environment) do</li>
</ol>
<p>在你的开发数据库上，运行这个任务，我将运行&ldquo;rake utils:send_expire_soon_emails&rdquo;。如果我想在产品数据库上运行，我将运行&ldquo;rake RAILS_ENV=production utils:send_expire_soon_emails&rdquo;。</p>
<p>如果我想每天午夜，在产品数据库上运行此任务，我将编写一个如下cronjob：</p>
<div class="wp_syntax">
<div class="code">
<pre class="text">0 0 * * * cd /var/www/apps/rails_app/ <br />  &amp;&amp; /usr/local/bin/rake RAILS_ENV=production utils:send_expire_soon_emails</pre>
</div>
</div>
<p>相当简便！</p>
<h2>哪里可以找到更多的例子？<a name="useful_resources"></a></h2>
<p>既然你已经知道如何开始编写rake任务，我想我应该给你一些资源。改善你编程最好的方法是阅读他人的代码，一些人们已经些好的rake任务。</p>
<ul>
    <li>这些Edge Rails中的<a href="http://dev.rubyonrails.org/changeset/6849">全新rake任务</a>帮助你创建、重置你的数据库。.</li>
    <li>Craig Ambrose写了一个Rake任务来<a href="http://blog.craigambrose.com/articles/2007/03/01/a-rake-task-for-database-backups">备份数据库</a>，你可以使用。</li>
    <li>Adam Greene把<a href="http://www.rubyinside.com/advent2006/15-s3rake.html">一些Rake任务</a>放在一起，允许你备份你的数据到Amazon S3。他给我发送那些库的更新版本，你可以在<a href="http://www.railsenvy.com/assets/2007/6/19/s3_rake_tasks.zip">这里</a>下载。</li>
    <li>Jay Fields讨论了<a href="http://blog.jayfields.com/2006/11/ruby-testing-rake-tasks.html">rake任务测试</a>，因此获得好评。</li>
    <li>Err the blog 讨论了关于<a href="http://errtheblog.com/post/33">设置RAILS ENV的新方法，并且教授如何运用rake来启动Mysql shell</a>（当你浏览的时候，确保阅读注释）。</li>
    <li>最后的但同等主要的，有<a href="http://docs.rubyrake.org/">Rake Bookshelf Books</a>和Martin Fowler的<a href="http://www.martinfowler.com/articles/rake.html">Using the Rake Build Language</a>教程。它们都很完备，但也有一点过时。</li>
</ul>
<p>如果你找到了其它比较好的，发贴子给我们。</p>
<h2>追加</h2>
<p>作为追加，我几分钟以前受到Jim的电子邮件，解释我如何可以简化我的目录创建脚本，相当酷：</p>
<table>
    <tbody>
        <tr>
            <td class="line_numbers">
            <pre>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></pre>
            </td>
            <td class="code">
            <pre class="ruby"><span style="color: rgb(0, 128, 0); font-style: italic;"># This is needed because the existing version of </span><br /><span style="color: rgb(0, 128, 0); font-style: italic;"># directory in Rake is slightly broken, but Jim says </span><br /><span style="color: rgb(0, 128, 0); font-style: italic;"># it'll be fixed in the next version.</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">alias</span> <span style="color: rgb(255, 51, 51); font-weight: bold;">:original_directory</span> <span style="color: rgb(255, 51, 51); font-weight: bold;">:directory</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">def</span> directory<span style="color: rgb(0, 102, 0); font-weight: bold;">(</span>dir<span style="color: rgb(0, 102, 0); font-weight: bold;">)</span><br />  original_directory dir<br />  <span style="color: rgb(102, 102, 255); font-weight: bold;">Rake::Task</span><span style="color: rgb(0, 102, 0); font-weight: bold;">[</span>dir<span style="color: rgb(0, 102, 0); font-weight: bold;">]</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span><br />&nbsp;<br /><span style="color: rgb(0, 128, 0); font-style: italic;"># Do the directory creation</span><br />namespace <span style="color: rgb(255, 51, 51); font-weight: bold;">:utils</span> <span style="color: rgb(153, 102, 204); font-weight: bold;">do</span><br />  task <span style="color: rgb(255, 51, 51); font-weight: bold;">:create_directories</span> =&gt; <span style="color: rgb(0, 102, 0); font-weight: bold;">[</span><br />    directory<span style="color: rgb(0, 102, 0); font-weight: bold;">(</span><span style="color: rgb(153, 102, 0);">'public/icons'</span><span style="color: rgb(0, 102, 0); font-weight: bold;">)</span>,<br />    directory<span style="color: rgb(0, 102, 0); font-weight: bold;">(</span><span style="color: rgb(153, 102, 0);">'public/images'</span><span style="color: rgb(0, 102, 0); font-weight: bold;">)</span>,<br />    directory<span style="color: rgb(0, 102, 0); font-weight: bold;">(</span><span style="color: rgb(153, 102, 0);">'public/groups'</span><span style="color: rgb(0, 102, 0); font-weight: bold;">)</span>,<br />  <span style="color: rgb(0, 102, 0); font-weight: bold;">]</span><br /><span style="color: rgb(153, 102, 204); font-weight: bold;">end</span></pre>
            </td>
        </tr>
    </tbody>
</table>
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/138787#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Wed, 07 Nov 2007 15:18:50 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/138787</link>
        <guid>http://jonny131.javaeye.com/blog/138787</guid>
      </item>
          <item>
        <title>在rjs里填充select 的options</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/135694" style="color:red;">http://jonny131.javaeye.com/blog/135694</a>&nbsp;
          发表时间: 2007年10月26日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          网上一般都说是用render的方法，不过那样需要一个template,我这个也比较简单就一句话<br />
<div class="code_title">ruby 代码</div>
<div class="dp-highlighter">
<div class="bar">&nbsp;</div>
<ol class="dp-rb" start="1">
    <li class="alt"><span><span>page.replace_html&nbsp;'wall_blog_title',&nbsp;&nbsp;options_for_select(</span><span class="variable">@items</span><span>)&nbsp;&nbsp;</span></span></li>
</ol>
</div>
@items 是准备好的数据，只要replace一下就好了。
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/135694#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Fri, 26 Oct 2007 11:40:11 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/135694</link>
        <guid>http://jonny131.javaeye.com/blog/135694</guid>
      </item>
          <item>
        <title>link_to_remote 如何通过 javascript传值的问题</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/135433" style="color:red;">http://jonny131.javaeye.com/blog/135433</a>&nbsp;
          发表时间: 2007年10月25日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          今天遇到一个问题，当用到link_to_remote的时候想把一个文本框中的值同时传给后台，试了好多种方法，折腾了我一个多小时。<br />
方法如下：<br />
<div class="code_title">ruby 代码</div>
<div class="dp-highlighter">
<div class="bar">&nbsp;</div>
<ol class="dp-rb" start="1">
    <li class="alt"><span><span>&lt;%=link_to_remote&nbsp;</span><span class="string">&quot;获取&quot;</span><span>,&nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="symbol">:url</span><span>=&gt;{</span><span class="symbol">:action</span><span>=&gt;</span><span class="string">&quot;fillRsstitle&quot;</span><span>},&nbsp;&nbsp;</span></span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="symbol">:with</span><span>=&gt;</span><span class="string">&quot;'uri='+encodeURIComponent($j('#wall_w_title').get(0).value)&quot;</span><span>&nbsp;&nbsp;</span></span></li>
    <li class=""><span>%&gt;&nbsp;&nbsp;</span></li>
</ol>
</div>
这是一个简单的link_to_remote代码，传递参数是通过:with 进行的，那主要的是如何正确执行javascript代码,<br />
<span><span class="string">encodeURIComponent($j('#wall_w_title').get(0).value)就是返回参数&quot;uri&quot;</span></span>的值，<span><span class="string">encodeURIComponent方法它是将包含中文、韩文等特殊字符转换成utf-8格式的url编码方法，方法里面的代码使用的是jquery的，实际等同于document.getElementById(&quot;wall_w_title&quot;).value.这样在后台就可以通过&nbsp; params[:uri]正确的得到传递的值了.</span></span><font size="4"><font color="#ff0000"><span twffan="done"></span><span twffan="done"></span></font></font>
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/135433#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Thu, 25 Oct 2007 14:12:46 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/135433</link>
        <guid>http://jonny131.javaeye.com/blog/135433</guid>
      </item>
          <item>
        <title>读取RSS的问题</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/135259" style="color:red;">http://jonny131.javaeye.com/blog/135259</a>&nbsp;
          发表时间: 2007年10月24日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          我用rss ＝RSS::Parser.parse(content,false)语句读取的rss发现有一个地址里面有的items的title读不出来，我调试后发现他的类型是String 值是[binary object]，不知到有没有人碰到过这个问题
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/135259#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Wed, 24 Oct 2007 18:12:49 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/135259</link>
        <guid>http://jonny131.javaeye.com/blog/135259</guid>
      </item>
          <item>
        <title>oracle 中删 除重复项</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/114892" style="color:red;">http://jonny131.javaeye.com/blog/114892</a>&nbsp;
          发表时间: 2007年08月21日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          ---create table category_keyword_bak as select * from category_keyword;<br />
delete from category_keyword where rowid not in (select min(rowid) from&nbsp; category_keyword group by keyword_id,category_id)
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/114892#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Tue, 21 Aug 2007 15:23:02 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/114892</link>
        <guid>http://jonny131.javaeye.com/blog/114892</guid>
      </item>
          <item>
        <title>oracle sequence</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/99863" style="color:red;">http://jonny131.javaeye.com/blog/99863</a>&nbsp;
          发表时间: 2007年07月10日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          在ORACLE中sequence就是所谓的序列号，每次取的时候它会自动增加，一般用在需要按序列号排序的地方。&nbsp;&nbsp;<br />
1、Create Sequence&nbsp;&nbsp;<br />
你首先要有CREATE SEQUENCE或者CREATE ANY SEQUENCE权限，&nbsp;&nbsp;<br />
CREATE SEQUENCE emp_sequence&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INCREMENT BY 1&nbsp;&nbsp;-- 每次加几个&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; START WITH 1&nbsp; &nbsp; -- 从1开始计数&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NOMAXVALUE&nbsp; &nbsp;&nbsp; &nbsp;-- 不设置最大值&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NOCYCLE&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;-- 一直累加，不循环&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CACHE 10;&nbsp;&nbsp;<br />
一旦定义了emp_sequence，你就可以用CURRVAL，NEXTVAL&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; CURRVAL=返回sequence的当前值&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; NEXTVAL=增加sequence的值，然后返回sequence值&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; 比如：&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; emp_sequence.CURRVAL&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; emp_sequence.NEXTVAL&nbsp;&nbsp;<br />
可以使用sequence的地方：&nbsp;&nbsp;<br />
- 不包含子查询、snapshot、VIEW的 SELECT 语句&nbsp;&nbsp;<br />
- INSERT语句的子查询中&nbsp;&nbsp;<br />
- NSERT语句的VALUES中&nbsp;&nbsp;<br />
- UPDATE 的 SET中&nbsp; &nbsp; <br />
可以看如下例子：&nbsp;&nbsp;<br />
INSERT INTO emp VALUES&nbsp; &nbsp;<br />
(empseq.nextval, 'LEWIS', 'CLERK',7902, SYSDATE, 1200, NULL, 20);&nbsp;&nbsp;<br />
SELECT empseq.currval FROM DUAL;&nbsp;&nbsp;<br />
但是要注意的是：&nbsp;&nbsp;<br />
- 第一次NEXTVAL返回的是初始值；随后的NEXTVAL会自动增加你定义的INCREMENT BY值，然后返回增加后的值。CURRVAL 总是返回当前SEQUENCE的值，但是在第一次NEXTVAL初始化之后才能使用CURRVAL，否则会出错。一次NEXTVAL会增加一次 SEQUENCE的值，所以如果你在同一个语句里面使用多个NEXTVAL，其值就是不一样的。明白？&nbsp;&nbsp;<br />
- 如果指定CACHE值，ORACLE就可以预先在内存里面放置一些sequence，这样存取的快些。cache里面的取完后，oracle自动再取一组 到cache。 使用cache或许会跳号， 比如数据库突然不正常down掉（shutdown abort),cache中的sequence就会丢失. 所以可以在create sequence的时候用nocache防止这种情况。&nbsp;&nbsp;<br />
2、Alter Sequence&nbsp;&nbsp;<br />
你或者是该sequence的owner，或者有ALTER ANY SEQUENCE 权限才能改动sequence. 可以alter除start至以外的所有sequence参数.如果想要改变start值，必须 drop&nbsp;&nbsp;sequence 再 re-create .&nbsp;&nbsp;<br />
Alter sequence 的例子&nbsp;&nbsp;<br />
ALTER SEQUENCE emp_sequence&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INCREMENT BY 10&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MAXVALUE 10000&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CYCLE&nbsp; &nbsp; -- 到10000后从头开始&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NOCACHE ;&nbsp;&nbsp;<br />
影响Sequence的初始化参数：&nbsp;&nbsp;<br />
SEQUENCE_CACHE_ENTRIES =设置能同时被cache的sequence数目。&nbsp; &nbsp;<br />
可以很简单的Drop Sequence&nbsp;&nbsp;<br />
DROP SEQUENCE order_seq;
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/99863#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Tue, 10 Jul 2007 19:43:57 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/99863</link>
        <guid>http://jonny131.javaeye.com/blog/99863</guid>
      </item>
          <item>
        <title>FC6 Intel 显卡驱动的安装</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/86752" style="color:red;">http://jonny131.javaeye.com/blog/86752</a>&nbsp;
          发表时间: 2007年06月04日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 公司发了个本本，以前因为自己机子实在太破硬盘又很小就没有装LINUX,可是让我逮到机会了，哈哈。安装时FC6就不认945GM 的集成显卡，文本模式装完后开始装驱动。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 接好网卡，命令提示符下<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # yum install 915resolution<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 安装好后输入：<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  #915resolution -l<br />
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 看看有没有支持的模式，我的机器的模式就没有在上面，我的是宽屏，输入：<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; # <span style="font-family: monospace;">915resolution 5c 1440X900 32<br />
<br />
&nbsp;&nbsp;&nbsp; 然后再在</span>/etc/X11/xorg.conf 文件中Screen的Display模式中增加几个&quot;1440x900&quot; &quot;1280x800&quot;,保存后再运行<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  #startx<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 终于进入Xwindow界面了。
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/86752#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 04 Jun 2007 23:01:40 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/86752</link>
        <guid>http://jonny131.javaeye.com/blog/86752</guid>
      </item>
          <item>
        <title>外键对性能的影响</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/80668" style="color:red;">http://jonny131.javaeye.com/blog/80668</a>&nbsp;
          发表时间: 2007年05月17日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          外键约束会对性能有一些影响，那具体有什么影响呢，我先写一个:<br />
1.往子表中插入一条数据，首先检查主表中是否有相应的主键值，锁定附表的记录，子表中插入值。多了两部操作，速度应该会慢一些。<br />
不知道在查询的情况下会不会有影响呢？
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/80668#comments" style="color:red;">已有 <strong>2</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Thu, 17 May 2007 18:48:56 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/80668</link>
        <guid>http://jonny131.javaeye.com/blog/80668</guid>
      </item>
          <item>
        <title>电话面试</title>
        <author>小小龙猫</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://jonny131.javaeye.com">小小龙猫</a>&nbsp;
                    链接：<a href="http://jonny131.javaeye.com/blog/80293" style="color:red;">http://jonny131.javaeye.com/blog/80293</a>&nbsp;
          发表时间: 2007年05月16日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          &nbsp;&nbsp;&nbsp;&nbsp; 今天接到电话面试，起先问的问题都还可以,之后问了我一个什么叫门面模式，我倒是用过模式，不过没用过门面模式，哪位大哥知道啊？<br />
<br />
definiton:<br />
&nbsp;&nbsp;&nbsp; 为子系统中的多个接口提供一个统一的接口，Facade 定义了一个更高层的接口以方便子系统更方便的应用。<br />
<br />
<h3 id="uml-scroll">UML class diagram</h3>
<br />
<img src="http://www.dofactory.com/Patterns/Diagrams/facade.gif" alt="UML Image" /><br />
<br />
<h3 id="csharp-scroll">sample code in C#</h3>
<font color="blue">using</font> System;<br />
<br />
<font color="blue">namespace</font> DoFactory.GangOfFour.Facade.Structural<br />
{<br />
<br />
&nbsp;&nbsp;<font color="green">// Mainapp test application </font><br />
<br />
&nbsp;&nbsp;<font color="blue">class</font> MainApp<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color="blue">public</font> <font color="blue">static</font> <font color="blue">void</font> Main()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Facade facade = <font color="blue">new</font> Facade();<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;facade.MethodA();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;facade.MethodB();<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="green">// Wait <font color="green">for</font> user </font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.Read();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;<font color="green">// &quot;Subsystem ClassA&quot;  </font><br />
<br />
&nbsp;&nbsp;<font color="blue">class</font> SubSystemOne<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color="blue">public</font> <font color="blue">void</font> MethodOne()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(&quot; SubSystemOne Method&quot;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;<font color="green">// Subsystem ClassB&quot; </font><br />
<br />
&nbsp;&nbsp;<font color="blue">class</font> SubSystemTwo<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color="blue">public</font> <font color="blue">void</font> MethodTwo()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(&quot; SubSystemTwo Method&quot;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;<font color="green">// Subsystem ClassC&quot; </font><br />
<br />
&nbsp;&nbsp;<font color="blue">class</font> SubSystemThree<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color="blue">public</font> <font color="blue">void</font> MethodThree()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(&quot; SubSystemThree Method&quot;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;<font color="green">// Subsystem ClassD&quot; </font><br />
<br />
&nbsp;&nbsp;<font color="blue">class</font> SubSystemFour<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color="blue">public</font> <font color="blue">void</font> MethodFour()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(&quot; SubSystemFour Method&quot;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;<font color="green">// &quot;Facade&quot; </font><br />
<br />
&nbsp;&nbsp;<font color="blue">class</font> Facade<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;SubSystemOne one;<br />
&nbsp;&nbsp;&nbsp;&nbsp;SubSystemTwo two;<br />
&nbsp;&nbsp;&nbsp;&nbsp;SubSystemThree three;<br />
&nbsp;&nbsp;&nbsp;&nbsp;SubSystemFour four;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color="blue">public</font> Facade()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;one = <font color="blue">new</font> SubSystemOne();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;two = <font color="blue">new</font> SubSystemTwo();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;three = <font color="blue">new</font> SubSystemThree();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;four = <font color="blue">new</font> SubSystemFour();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color="blue">public</font> <font color="blue">void</font> MethodA()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(&quot;\nMethodA() ---- &quot;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;one.MethodOne();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;two.MethodTwo();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;four.MethodFour();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color="blue">public</font> <font color="blue">void</font> MethodB()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(&quot;\nMethodB() ---- &quot;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;two.MethodTwo();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;three.MethodThree();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
}&nbsp;&nbsp;&nbsp;&nbsp;<br />
<br />
<br />
<font size="2" face="arial, helvetica">Output</font><br />
MethodA() ----<br />
SubSystemOne Method<br />
SubSystemTwo Method<br />
SubSystemFour Method<br />
<br />
MethodB() ----<br />
SubSystemTwo Method<br />
SubSystemThree Method
          <br/><br/>
          <span style="color:red;">
            <a href="http://jonny131.javaeye.com/blog/80293#comments" style="color:red;">已有 <strong>3</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Wed, 16 May 2007 11:16:44 +0800</pubDate>
        <link>http://jonny131.javaeye.com/blog/80293</link>
        <guid>http://jonny131.javaeye.com/blog/80293</guid>
      </item>
      </channel>
</rss>