<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>程序员 in the house &#187; Ruby&amp;Rails</title>
	<atom:link href="http://blog.thinklet.net/franky/category/rubyrails/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thinklet.net/franky</link>
	<description>跳街舞比我劲的，写程序没我历害；写程序比我历害的，跳街舞比不上我。</description>
	<lastBuildDate>Sun, 18 Apr 2010 16:20:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>十个不可或缺的ruby gems</title>
		<link>http://blog.thinklet.net/franky/2010/02/09/shigebukehuoquederubygems/</link>
		<comments>http://blog.thinklet.net/franky/2010/02/09/shigebukehuoquederubygems/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 14:48:23 +0000</pubDate>
		<dc:creator>frankychen</dc:creator>
				<category><![CDATA[Ruby&Rails]]></category>

		<guid isPermaLink="false">http://blog.thinklet.net/franky/?p=171</guid>
		<description><![CDATA[在我编程的时候有些东西是必须的。 于是我总结了这个 10 个最重要的 ruby gems 列表， 它们将使你的生活变得更轻松， 即使你只是花少量的时间用 ruby 编程。
　　Gems 有很多， 不过这些都是精华!
　　capistrano
　　Capistrano 是 Rails 应用程序开发更加从容。 它还是一个用来和远程服务器交互的聪明的 framework 。 事实上我已经备份了用 Capistrano 写的代码， 作为 Rake/Net::SSH 或者 shell 代码之外的另一个选择。
　　chronic
　　rubyforge.org/&#8221;&#62;Chronic 是一个自然语言分析器， 它的名字是个俏皮的双关。
　　&#62;&#62; Chronic.parse(&#8217;3rd wednesday in november&#8217;)=&#62; Wed Nov 19 12:00:00 0000 2008ferret
　　Ferret 会让你的老板、客户爱死你。 它提供了快速文本查询的工具， 你甚至可以可以给 PDF 文件和 Office 文档加索引。 我已经在很多网站上使用 Ferret 了， 我的客户们都说：“ 哇， 这看起来像 Google!”
　　hpricot
　　hpricot 是一个快速、容易上手的 HTML [...]]]></description>
			<content:encoded><![CDATA[<p>在我编程的时候有些东西是必须的。 于是我总结了这个 10 个最重要的 ruby gems 列表， 它们将使你的生活变得更轻松， 即使你只是花少量的时间用 ruby 编程。</p>
<p>　　Gems 有很多， 不过这些都是精华!</p>
<p>　　capistrano</p>
<p>　　Capistrano 是 Rails 应用程序开发更加从容。 它还是一个用来和远程服务器交互的聪明的 framework 。 事实上我已经备份了用 Capistrano 写的代码， 作为 Rake/Net::SSH 或者 shell 代码之外的另一个选择。</p>
<p>　　chronic</p>
<p>　　rubyforge.org/&#8221;&gt;Chronic 是一个自然语言分析器， 它的名字是个俏皮的双关。</p>
<p>　　&gt;&gt; Chronic.parse(&#8217;3rd wednesday in november&#8217;)=&gt; Wed Nov 19 12:00:00 0000 2008ferret</p>
<p>　　Ferret 会让你的老板、客户爱死你。 它提供了快速文本查询的工具， 你甚至可以可以给 PDF 文件和 Office 文档加索引。 我已经在很多网站上使用 Ferret 了， 我的客户们都说：“ 哇， 这看起来像 Google!”</p>
<p>　　hpricot</p>
<p>　　hpricot 是一个快速、容易上手的 HTML 分析器。 如果你发现自己需要解析 HTML 抑或 XML 的内容， hpricot 是最简捷的方法之一。 事实上我在 Helipad 中的很多地方使用了这个工具。</p>
<p>　　rcov</p>
<p>　　rcov 是使你的程序不经常崩溃的理由之一。 它分析你的代码， 高亮显示你的测试代码遗漏的部分。 它速度快， 不占资源， 并且能减轻头痛 ：)</p>
<p>　　scruffy</p>
<p>　　我一直用 Gruff 来画图， 不过当 scruffy 很完善的时候， 我更喜欢 rubyforge.org/&#8221;&gt;scruffy API 的设计。 它工作的方式非常精妙： 产生简单的 XML 来驱动 SVG 图。 因为 SVG 提供了一直直观地处理原始图片的方式， 你可以丢掉那些大量杂乱难懂的代码了。</p>
<p>　　我经常继承和重写 scruffy 来做成各种精巧的图片， 如果你觉得你现在用类库不太灵， 何不试试 scruffy 呢。</p>
<p>　　simple-rss</p>
<p>　　rubyforge.org/&#8221;&gt;simple-rss 分析 RSS 和 atom feed， 干的又快又好。 它的分析器也容易扩展， 这样你就可以应付那些无效的或专有的 feeds。</p>
<p>　　thin</p>
<p>　　Thin 使用 EventMachine， Rack 和 Mongrel 来创建一个快速的 web server。 我用它来做 Rails 的本地开发， 它比 mongrel 稍快。 要在我的 Rails 程序中使用它， 只需要在我的程序目录下输入 thin start 就行了。 当时我的 thin 和 IE cookies 之间有点儿冲突， 要记得如果你也遇到同样的问题， 你的程序可能有麻烦。 (说明: 这个问题在 thin 0.6.1 和 Rails 2 环境下好像修正了。)</p>
<p>　　要安装 thin， 使用 gem install thin 命令。</p>
<p>　　turn</p>
<p>　　Turn 使你的 Test::Unit 输出更易读。 Turn 也行看起来不那么招人喜欢， 但它确实对阅读测试结果有很大帮助。 在维护 Helicoid 的时候我对测试严重依赖， 所以任何能使这项工作变得更简洁的东西我都乐于尝试。</p>
<p>　　要安装 turn， 使用 gem install turn 命令。</p>
<p>　　wirble</p>
<p>　　wirble 使 irb 用起来更简单。 你有更好的对 history 的支持(在 session 间)以及颜色， 所以在繁重的工作中，你的眼睛和手指能得到休息。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thinklet.net/franky/2010/02/09/shigebukehuoquederubygems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails性能优化之 ruby解析器的优化</title>
		<link>http://blog.thinklet.net/franky/2009/09/29/railsxingnengyouhuazhirubyjiexiqideyouhua/</link>
		<comments>http://blog.thinklet.net/franky/2009/09/29/railsxingnengyouhuazhirubyjiexiqideyouhua/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 16:37:15 +0000</pubDate>
		<dc:creator>frankychen</dc:creator>
				<category><![CDATA[Ruby&Rails]]></category>

		<guid isPermaLink="false">http://blog.thinklet.net/franky/?p=145</guid>
		<description><![CDATA[来自己javaeye的原文：http://robbin.javaeye.com/blog/157731
ruby的解析器性能是很糟糕的，ruby早期的主要用途是取代perl写批量处理的脚本的，并不是为服务器应用编写的，因此在内存分配策略上非常不适合服务器应用。Stefans Kaes编写了一个ruby GC的补丁文件，在railsbench下载包里面提供了。虽然当前Railsbench提供的GC补丁只有针对ruby 1.8.4和1.8.5版本的，但是在ruby 18,6上面使用1.8.5的GC补丁也完全没有问题。GC补丁的作用主要是针对Rails应用开大了ruby的内存堆，可以有效提高内存堆的利用率，降低GC的频率。根据Stefans Kaes提供的测试数据，打补丁并且调整参数以后，GC的频率下降到只有原来的1/10还不到。降低GC频率尽管并不能够提高单个请求的执行速度，但是可以增加整体应用的负载能力。 
JavaEye的服务器上也使用了GC补丁，并且根据推荐参数进行了调整。在使用GC补丁之后，Web服务器的CPU负载下降了大概15%左右，效果非常显著。当然开大内存堆的代价就是ruby进程会多消耗内存，在我们的服务器上，ruby打补丁之后多消耗了50%左右的物理内存。 
]]></description>
			<content:encoded><![CDATA[<p>来自己javaeye的原文：http://robbin.javaeye.com/blog/157731</p>
<p>ruby的解析器性能是很糟糕的，ruby早期的主要用途是取代perl写批量处理的脚本的，并不是为服务器应用编写的，因此在内存分配策略上非常不适合服务器应用。Stefans Kaes编写了一个ruby GC的补丁文件，在railsbench下载包里面提供了。虽然当前Railsbench提供的GC补丁只有针对ruby 1.8.4和1.8.5版本的，但是在ruby 18,6上面使用1.8.5的GC补丁也完全没有问题。GC补丁的作用主要是针对Rails应用开大了ruby的内存堆，可以有效提高内存堆的利用率，降低GC的频率。根据Stefans Kaes提供的测试数据，打补丁并且调整参数以后，GC的频率下降到只有原来的1/10还不到。降低GC频率尽管并不能够提高单个请求的执行速度，但是可以增加整体应用的负载能力。 </p>
<p>JavaEye的服务器上也使用了GC补丁，并且根据推荐参数进行了调整。在使用GC补丁之后，Web服务器的CPU负载下降了大概15%左右，效果非常显著。当然开大内存堆的代价就是ruby进程会多消耗内存，在我们的服务器上，ruby打补丁之后多消耗了50%左右的物理内存。 </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thinklet.net/franky/2009/09/29/railsxingnengyouhuazhirubyjiexiqideyouhua/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails常用命令整理</title>
		<link>http://blog.thinklet.net/franky/2009/04/13/rails%e5%b8%b8%e7%94%a8%e5%91%bd%e4%bb%a4%e6%95%b4%e7%90%86/</link>
		<comments>http://blog.thinklet.net/franky/2009/04/13/rails%e5%b8%b8%e7%94%a8%e5%91%bd%e4%bb%a4%e6%95%b4%e7%90%86/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 13:45:25 +0000</pubDate>
		<dc:creator>frankychen</dc:creator>
				<category><![CDATA[Ruby&Rails]]></category>
		<category><![CDATA[rake;ruby on rails;ruby]]></category>

		<guid isPermaLink="false">http://blog.thinklet.net/franky/?p=110</guid>
		<description><![CDATA[&#8212;&#8212;
GEM：
gem install rails &#8211;include-dependencies
gem install rails -v=2.0.2
自动安装rails
&#8212;&#8212;
1.1 创建Rails应用程序
$ rails [_VERSION_] app_name
可选项:
VERSION: 指定rails的版本(如果安装了多个rails版本),VERSION前后各加一下划线, 如&#8221;_1.2.6_&#8221;
-d, database=xxx 指定安装一个数据库(mysql oracle postgresql sqlite2 sqlite3 ), 默认情况下是数据库
-r, ruby-path= 指定Ruby的安装路径,如果没有指定，scripts使用env去找Ruby
-f, freeze (冻结)freezes Rails在vendor/rails目录
1.2 API Documentation
$ gem_server
启动一个WEBrick服务器。这时候你可以通过Http://localhost:8808/ 打开浏览器去查看rails API文档
1.3 Rake
rake db:fixtures:load
# 载入fixtures到当前环境的数据库
# 载入指定的fixtures使用FIXTURES=x,y
rake db:migrate
# 迁移数据库通过在db/migrate目录下的脚本.可以指定版本号通过VERSION=x
&#8212;&#8212;-
    * db:charset 检索当前环境下数据库的字符设置
    * db:collation 检索当前环境下数据库的校对
    * db:create 用config\database.yml中的定义创建当前 [...]]]></description>
			<content:encoded><![CDATA[<p>&#8212;&#8212;<br />
GEM：<br />
gem install rails &#8211;include-dependencies<br />
gem install rails -v=2.0.2<br />
自动安装rails</p>
<p>&#8212;&#8212;</p>
<p>1.1 创建Rails应用程序<br />
$ rails [_VERSION_] app_name<br />
可选项:<br />
VERSION: 指定rails的版本(如果安装了多个rails版本),VERSION前后各加一下划线, 如&#8221;_1.2.6_&#8221;<br />
-d, database=xxx 指定安装一个数据库(mysql oracle postgresql sqlite2 sqlite3 ), 默认情况下是数据库<br />
-r, ruby-path= 指定Ruby的安装路径,如果没有指定，scripts使用env去找Ruby<br />
-f, freeze (冻结)freezes Rails在vendor/rails目录</p>
<p>1.2 API Documentation<br />
$ gem_server<br />
启动一个WEBrick服务器。这时候你可以通过Http://localhost:8808/ 打开浏览器去查看rails API文档</p>
<p>1.3 Rake<br />
rake db:fixtures:load<br />
# 载入fixtures到当前环境的数据库<br />
# 载入指定的fixtures使用FIXTURES=x,y</p>
<p>rake db:migrate<br />
# 迁移数据库通过在db/migrate目录下的脚本.可以指定版本号通过VERSION=x<br />
&#8212;&#8212;-</p>
<p>    * db:charset 检索当前环境下数据库的字符设置<br />
    * db:collation 检索当前环境下数据库的校对<br />
    * db:create 用config\database.yml中的定义创建当前 RAILS_ENV 项目环境下的数据库<br />
    * db:create:all 用config\database.yml中的定义创建所有数据库<br />
    * db:drop 删除当前 RAILS_ENV项目环境中的数据库<br />
    * db:drop:all 删除所有在 config\database.yml中定义的数据库<br />
    * db:reset 从db\schema.rb中为当前环境重建数据库（先删后建）.<br />
    * db:rollback 回滚(清华出版社一本SQLSERVER书的名词[很奇怪为什么不直接用滚回])数据库到前一个版本. 指定回滚到哪一步要用 STEP=n 参数<br />
    * db:version 检索当前模式下的版本<br />
      &#8212;&#8212;-</p>
<p>rake db:schema:dump<br />
# 创建一个db/schema.rb文件，通过AR能过够支持任何数据库去使用</p>
<p>rake db:schema:load<br />
# 再入一个schema.rb文件进数据库</p>
<p>rake db:sessions:clear<br />
# 清空sessions表</p>
<p>rake db:sessions:create<br />
# 用CGI::Session::ActiveRecordStore创建一个sessions表为用户</p>
<p>rake db:structure:dump<br />
# 导出数据库结构为一个SQL文件</p>
<p>rake db:test:clone<br />
# 重新创建一个测试数据库从当前环境数据库中</p>
<p>rake db:test:clone_structure<br />
# 重新创建测试数据库从开发模式数据库</p>
<p>rake db:test:prepare<br />
# 准备测试数据库并在入schema</p>
<p>rake db:test:purge<br />
# 清空测试数据库</p>
<p>rake doc:app<br />
# 创建HTML文件的API Documentation</p>
<p>rake doc:clobber_app<br />
# 删除Documentation</p>
<p>rake doc:clobber_plugins<br />
# 删除 plugin Documentation</p>
<p>rake doc:clobber_rails<br />
# 删除Documentation</p>
<p>rake doc:plugins<br />
# 产生Documation为所有安装的plugins</p>
<p>rake doc:rails<br />
# 创建HTML文件的API Documentation</p>
<p>rake doc:reapp<br />
# 强制重新创建HTML文件的API Documentation</p>
<p>rake doc:rerails<br />
# 强制重新创建HTML文件的API Documentation</p>
<p>rake log:clear<br />
# 清空目录log/下的所有日志文件</p>
<p>rake rails:freeze:edge<br />
# Lock this application to latest Edge Rails. Lock a specific revision with REVISION=X</p>
<p>rake rails:freeze:gems<br />
# Lock this application to the current gems (by unpacking them into vendor/rails)</p>
<p>rake rails:unfreeze<br />
# Unlock this application from freeze of gems or edge and return to a fluid use of system gems</p>
<p>rake rails:update<br />
# Update both scripts and public/javascripts from Rails</p>
<p>rake rails:update:javascripts<br />
# Update your javascripts from your current rails install</p>
<p>rake rails:update:scripts<br />
# Add new scripts to the application script/ directory</p>
<p>rake stats<br />
# Report code statistics (KLOCs, etc) from the application</p>
<p>rake test<br />
# Test all units and functionals</p>
<p>rake test:functionals<br />
# Run tests for functionalsdb:test:prepare</p>
<p>rake test:integration<br />
# Run tests for integrationdb:test:prepare</p>
<p>rake test:plugins<br />
# Run tests for pluginsenvironment</p>
<p>rake test:recent<br />
# Run tests for recentdb:test:prepare</p>
<p>rake test:uncommitted<br />
# Run tests for uncommitteddb:test:prepare</p>
<p>rake test:units<br />
# Run tests for unitsdb:test:prepare</p>
<p>rake tmp:cache:clear<br />
# 清空tmp/cache目录下的所有文件</p>
<p>rake tmp:clear<br />
# 清空session, cache, 和socket文件从tmp/目录</p>
<p>rake tmp:create<br />
# 为sessions, cache, and sockets创建tmp/目录</p>
<p>rake tmp:sessions:clear<br />
# 清空所有在tmp/sessions目录下的文件</p>
<p>rake tmp:sockets:clear<br />
# 清空所有在tmp/sessions 目录下的ruby_sess.* 文件</p>
<p>rails 2.0新增的Rake Tasks<br />
rake routes：打印当前路由列表；</p>
<p>rake db:drop, rake db:drop:all, 删除当前环境或config/database.yml中的所有数据库；</p>
<p>rake db:create, rake db:create:all，创建当前环境数据库或或config/database.yml中的所有数据库；</p>
<p>rake db:version，显示当前环境数据库的schema版本号；</p>
<p>rake db:reset, 删除并重新创建当前环境的数据库。</p>
<p>1.4 Scripts<br />
script/about<br />
# 输出当前环境信息</p>
<p>script/breakpointer<br />
# 启动断点server</p>
<p>script/console<br />
# 启动交换式的Rails控制台</p>
<p>script/destroy<br />
# 删除通过generators创建的文件</p>
<p>script/generate<br />
# -&gt; generators</p>
<p>script/plugin<br />
# -&gt; Plugins</p>
<p>script/runner<br />
# 执行一个任务在rails上下文中</p>
<p>script/server<br />
# 启动开发模式服务器http://localhost:3000</p>
<p>//以下几个不知道怎么去使用<br />
script/performance/profiler<br />
script/performance/benchmarker<br />
script/process/reaper<br />
script/process/spawner</p>
<p>1.5 Generators</p>
<p>ruby script/generate model ModelName</p>
<p>ruby script/generate controller ListController show edit</p>
<p>ruby script/generate scaffold ModelName ControllerName</p>
<p>ruby script/generate migration AddNewTable</p>
<p>ruby script/generate plugin PluginName</p>
<p>ruby script/generate mailer Notification lost_password signup</p>
<p>ruby script/generate web_service ServiceName api_one api_two</p>
<p>ruby script/generate integration_test TestName</p>
<p>ruby script/generate session_migration</p>
<p>可选项:<br />
-p, &#8211;pretend Run but do not make any changes.<br />
-f, &#8211;force Overwrite files that already exist.<br />
-s, &#8211;skip Skip files that already exist.<br />
-q, &#8211;quiet Suppress normal output.<br />
-t, &#8211;backtrace Debugging: show backtrace on errors.<br />
-h, &#8211;help Show this help message.<br />
-c, &#8211;svn Modify files with subversion. (Note: svn must be in path)<br />
 &#8212;&#8212;&#8212;<br />
ruby script/generate scaffold helloworld<br />
生成某个Model的CRUD操作的应用框架</p>
<p>ruby script/destroy scaffold helloworld<br />
删除某个Model的CRUD操作的应用框架</p>
<p>1.6 Plugins</p>
<p>script/plugin discover<br />
# discover plugin repositories</p>
<p>script/plugin list<br />
# list all available plugins</p>
<p>script/plugin install where<br />
# install the a€wherea€? plugin</p>
<p>script/plugin install -x where<br />
# install where plugin as SVN external</p>
<p>script/plugin install http://invisible.ch/projects/plugins/where</p>
<p>script/plugin update<br />
# update installed plugins</p>
<p>script/plugin source<br />
# add a source repository</p>
<p>script/plugin unsource<br />
# removes a source repository</p>
<p>script/plugin sources<br />
# lists source repositories</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thinklet.net/franky/2009/04/13/rails%e5%b8%b8%e7%94%a8%e5%91%bd%e4%bb%a4%e6%95%b4%e7%90%86/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ruby on rails中Rake的经典教程</title>
		<link>http://blog.thinklet.net/franky/2009/03/01/ruby-on-rails%e4%b8%adrake%e7%9a%84%e7%bb%8f%e5%85%b8%e6%95%99%e7%a8%8b/</link>
		<comments>http://blog.thinklet.net/franky/2009/03/01/ruby-on-rails%e4%b8%adrake%e7%9a%84%e7%bb%8f%e5%85%b8%e6%95%99%e7%a8%8b/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 16:17:35 +0000</pubDate>
		<dc:creator>frankychen</dc:creator>
				<category><![CDATA[Ruby&Rails]]></category>
		<category><![CDATA[Web开发技巧]]></category>
		<category><![CDATA[rake;ruby on rails;ruby]]></category>

		<guid isPermaLink="false">http://blog.thinklet.net/franky/?p=69</guid>
		<description><![CDATA[最近在用rake结合svn来写一个Daily build报告。
我们平台的后台job大部份都是用rake来实现的，总结一下，用rake写后台job有以下一些好处：
1.  rake可以与ruby on rails紧密结合，很方便就可以调用rails框架的一些功能。如：调用model进行数据处理。
2. rake支持调用shell脚本，可以与shell结合使用。
3. ruby大量类库支持。
以下是关于rake的一个经典的教程
原文：Ruby on Rails Rake Tutorial (aka. How rake turned me into an alcoholic) 
引言：作为一个rails的开发者，你可能很熟悉使用rake进行你的测试，或者使用rake db:migrate运行你的migrations，但是你真的知道Rake的背后故事吗？你意识到可以自己写一个Rake任务或者一个有用的lib吗？
下面是我们使用Rake任务的例子：
1、给列表中的用户发送邮件
2、每晚数据的计算和报告
3、过期或重新生成缓存
4、备份数据和svn版本(how&#8217;s this : subversion repository)
5、运行数据处理脚本(sort of,how much is called this )
6、Pouring drinks to get a good buzz on(一句玩笑，是这两位仁兄的风格)

这篇文章中，我们将讨论为什么要创建Rake，和他怎么样帮助我们的rails应用。最好你可以写自己的Rake。
一、历史回顾：make
为了了解Rake的来历，我们先了解一下Rake的爷爷：Make。
让我们回到那个代码块需要编译，解释性语言和iphone还没出现在地球上的时代。

回到那时，我们下载的大型程序，还是一堆源代码和一个shell脚本。这个shell脚本包含了所有需要用来compile/link/build的代码。你需要运行“install_me.sh”这个脚本，每一行代码将被运行（编译每一行源文件），然后生成一个你能够运行的文件。
对于大多数人这样是不错的，但是对于程序开发人员却是一个不幸。每次你对源代码进行一个小的改动，并进行测试的时候，你需要回到shell脚本，并重新编译所有的源代码，显然对于大的程序“那是相当的”耗时的。
1977年（作者出生那年，我78年），贝尔实验室的Stuart Feldman创造了“make”。解决了编译时间过长的问题。Make用来编译程序，取得两方面的进步：

Stuart Feldman
（1）Make可以发现哪个文件在上一次编译后改动过，根据这点，再次运行Make时，仅编译改动过的文件。这个很大程序上减少了重新编译大型程序的时间。
（2）Make可以进行从属跟踪。你可以告诉编译器，源文件A的编译需要源文件B，源文件B的编译需要源文件C，所以Make在编译A时发现B没有编译，将会先编译B。
可以这样定义：Make是一个可执行程序。像ls或dir一样。让Make理解如何让编译一个项目，需要创建一个makefile文件，描述所有的源文件和依赖关系。makefiles有自己的语法，你不用去了解。
这些年Make出现了其他的变体，并且被其他的语言使用。事实上，ruby用户在rake出现前也在使用它。
“但是，ruby并不需要编译，我们用它来干嘛？”
是啊。ruby是一个解释性语言，我们不需要编译它的源代码，所以ruby程序员为什么使用它呢？
两个重要的原因：
（1）创建任务
在大型的应用中，你经常编写脚本，在命令行下运行一些任务。比如清除缓存，维护任务，或者迁移数据库。你可以写一个MakeFile来组织你的任务，而不是写十个不相干的脚本（或者一个复杂的）。这样你可以简单的运行：“make stupid”。
（2）从属任务跟踪
当你开始写一些维护任务的时候，可能发现有些任务的使用可能有重复。比如，“migrate”任务和“schema:dump”都需要链接数据库，这样我可以创建一个任务&#8221;connect_to_database&#8221;，使“migrate”和“schema:dump”都依赖于&#8221;connect_to_database&#8221;，这样下次运行“migrate”时，&#8221;connect_to_database&#8221;会先于“migrate”运行
二、如何得到Rake
几年前，Jim Weirich在一个java项目上使用了Make，他发现如果在他的Makefile中写一小段ruby代码将会带来非常大的方便。所以他创建了Rake。



左：Jim Weirich，中：Jason Seifer，右：Gregg Pollack
（后面两位为本文作者）
Jim 为Rake创建了任务功能，附属关系跟踪，甚至创建了时间段判断(timestamp recognition)，（在上一次编译的基础上仅编译改动的部分），当然，对于ruby，我们并不需要编译。
我很想知道Jim在代码里做了什么，你也想知道吧。Jim可能从来没想给这个代码写个文档，可能现在他也是被烦透了，写了一个。呵呵
三、Rake如何工作
开始我想给这个部分起名为&#8221;How to get wasted with Rake&#8221;。
那么我想喝点酒，该怎么做呢？
1、去买酒
2、喝酒
3、喝醉

如果我要使用Rake完成这个任务，我会创建一个“Rakefile”文件：
task :purchaseAlcohol do
  [...]]]></description>
			<content:encoded><![CDATA[<p>最近在用rake结合svn来写一个Daily build报告。<br />
我们平台的后台job大部份都是用rake来实现的，总结一下，用rake写后台job有以下一些好处：<br />
1.  rake可以与ruby on rails紧密结合，很方便就可以调用rails框架的一些功能。如：调用model进行数据处理。<br />
2. rake支持调用shell脚本，可以与shell结合使用。<br />
3. ruby大量类库支持。</p>
<p><strong>以下是关于rake的一个经典的教程</strong></p>
<p>原文：<a href="http://www.railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial" target="_blank">Ruby on Rails Rake Tutorial (aka. How rake turned me into an alcoholic) </a><br />
引言：作为一个rails的开发者，你可能很熟悉使用rake进行你的测试，或者使用rake db:migrate运行你的migrations，但是你真的知道Rake的背后故事吗？<span style="text-decoration: underline">你意识到可以自己写一个Rake任务或者一个有用的lib吗</span>？</p>
<p>下面是我们使用Rake任务的例子：</p>
<p>1、给列表中的用户发送邮件<br />
2、每晚数据的计算和报告<br />
3、过期或重新生成缓存<br />
4、备份数据和svn版本(<span style="text-decoration: underline">how&#8217;s this : subversion repository</span>)<br />
5、运行数据处理脚本(sort of,how much is called this )<br />
6、Pouring drinks to get a good buzz on(一句玩笑，是这两位仁兄的风格)</p>
<div><img class="blogimg" src="http://hiphotos.baidu.com/%D0%C7203/pic/item/b126f9fde8c7851008244dc1.jpg" border="0" alt="" /></div>
<p>这篇文章中，我们将讨论为什么要创建Rake，和他怎么样帮助我们的rails应用。最好你可以写自己的Rake。</p>
<p>一、历史回顾：make</p>
<p>为了了解Rake的来历，我们先了解一下Rake的爷爷：Make。<br />
让我们回到那个代码块需要编译，解释性语言和iphone还没出现在地球上的时代。</p>
<div><img class="blogimg" src="http://www.railsenvy.com/assets/2007/6/8/oldcomputers-small.jpg" border="0" alt="" /></div>
<p>回到那时，我们下载的大型程序，还是一堆源代码和一个shell脚本。这个shell脚本包含了所有需要用来compile/link/build的代码。你需要运行“install_me.sh”这个脚本，每一行代码将被运行（编译每一行源文件），然后生成一个你能够运行的文件。</p>
<p>对于大多数人这样是不错的，但是对于程序开发人员却是一个不幸。每次你对源代码进行一个小的改动，并进行测试的时候，你需要回到shell脚本，并重新编译所有的源代码，显然对于大的程序“那是相当的”耗时的。</p>
<p>1977年（作者出生那年，我78年），贝尔实验室的Stuart Feldman创造了“make”。解决了编译时间过长的问题。Make用来编译程序，取得两方面的进步：</p>
<div><a href="http://www.railsenvy.com/assets/2007/6/8/feldman_stuart.gif" target="_blank"><img class="blogimg" src="http://www.railsenvy.com/assets/2007/6/8/feldman_stuart.gif" border="0" alt="" width="144" height="200" /></a></div>
<p><strong>Stuart Feldman</strong></p>
<p>（1）Make可以发现哪个文件在上一次编译后改动过，根据这点，再次运行Make时，仅编译改动过的文件。这个很大程序上减少了重新编译大型程序的时间。</p>
<p>（2）Make可以进行从属跟踪。你可以告诉编译器，源文件A的编译需要源文件B，源文件B的编译需要源文件C，所以Make在编译A时发现B没有编译，将会先编译B。</p>
<p>可以这样定义：Make是一个可执行程序。像ls或dir一样。让Make理解如何让编译一个项目，需要创建一个makefile文件，描述所有的源文件和依赖关系。makefiles有自己的语法，你不用去了解。</p>
<p>这些年Make出现了其他的变体，并且被其他的语言使用。事实上，ruby用户在rake出现前也在使用它。</p>
<p>“但是，ruby并不需要编译，我们用它来干嘛？”</p>
<p>是啊。ruby是一个解释性语言，我们不需要编译它的源代码，所以ruby程序员为什么使用它呢？</p>
<p>两个重要的原因：</p>
<p>（1）创建任务<br />
在大型的应用中，你经常编写脚本，在命令行下运行一些任务。比如清除缓存，维护任务，或者迁移数据库。你可以写一个MakeFile来组织你的任务，而不是写十个不相干的脚本（或者一个复杂的）。这样你可以简单的运行：“make stupid”。</p>
<p>（2）从属任务跟踪<br />
当你开始写一些维护任务的时候，可能发现有些任务的使用可能有重复。比如，“migrate”任务和“schema:dump”都需要链接数据库，这样我可以创建一个任务&#8221;connect_to_database&#8221;，使“migrate”和“schema:dump”都依赖于&#8221;connect_to_database&#8221;，这样下次运行“migrate”时，&#8221;connect_to_database&#8221;会先于“migrate”运行</p>
<p>二、如何得到Rake</p>
<p>几年前，<a href="http://onestepback.org/" target="_new">Jim Weirich</a>在一个java项目上使用了Make，他发现如果在他的Makefile中<strong>写一小段ruby代码</strong>将会带来非常大的方便。所以他创建了Rake。</p>
<div><a href="http://www.railsenvy.com/assets/2007/6/8/jim_1.jpg" target="_blank"></p>
<div><img class="blogimg" src="http://www.railsenvy.com/assets/2007/6/8/jim_1.jpg" border="0" alt="" /></div>
<p></a><br />
左：Jim Weirich，中：Jason Seifer，右：Gregg Pollack<br />
（后面两位为本文作者）</div>
<p>Jim 为Rake创建了任务功能，附属关系跟踪，甚至创建了时间段判断(timestamp recognition)，（在上一次编译的基础上仅编译改动的部分），当然，对于ruby，我们并不需要编译。</p>
<p>我很想知道Jim在代码里做了什么，你也想知道吧。Jim可能从来没想给这个代码写个文档，可能现在他也是被烦透了，<a href="http://rake.rubyforge.org/files/doc/rational_rdoc.html" target="_blank">写了一个</a>。呵呵</p>
<p>三、Rake如何工作</p>
<p>开始我想给这个部分起名为&#8221;How to get wasted with Rake&#8221;。</p>
<p>那么我想喝点酒，该怎么做呢？</p>
<p>1、去买酒<br />
2、喝酒<br />
3、喝醉</p>
<div><img class="blogimg" src="http://www.railsenvy.com/assets/2007/6/8/me_drink_1.jpg?1181337324" border="0" alt="" /></div>
<p>如果我要使用Rake完成这个任务，我会创建一个“Rakefile”文件：</p>
<pre>task <span class="sy">:purchaseAlcohol</span> <span class="r">do</span><tt>
</tt>  puts <span class="s"><span class="dl">"</span><span class="k">Purchased Vodka</span><span class="dl">"</span></span><tt>
</tt><span class="r">end</span><tt>
</tt><tt>
</tt>task <span class="sy">:mixDrink</span> <span class="r">do</span><tt>
</tt>  puts <span class="s"><span class="dl">"</span><span class="k">Mixed Fuzzy Navel</span><span class="dl">"</span></span><tt>
</tt><span class="r">end</span><tt>
</tt><tt>
</tt>task <span class="sy">:getSmashed</span> <span class="r">do</span><tt>
</tt>  puts <span class="s"><span class="dl">"</span><span class="k">Dood, everthing's blurry, can I halff noth'r drinnnk?</span><span class="dl">"</span></span><tt>
</tt><span class="r">end</span></pre>
<p>这样我可以在这个Rakefile的目录，分别运行这些任务：<br />
</p>
<pre><span class="er">$</span> rake purchaseAlcohol<tt>
</tt> <span class="co">Purchased</span> <span class="co">Vodka</span><tt>
</tt><span class="er">$</span> rake mixDrink<tt>
</tt> <span class="co">Mixed</span> <span class="co">Fuzzy</span> <span class="co">Navel</span><tt>
</tt><span class="er">$</span> rake getSmashed<tt>
</tt> <span class="co">Dood</span>, everthing<span class="s"><span class="dl">'</span><span class="k">s blurry, can I halff noth</span><span class="dl">'</span></span>r drinnnk?</pre>
<p>酷！但是从顺序上看，我可以用任何的顺序运行这个任务。比如喝醉在买酒或者喝酒之前。当然这不符合人的习惯。</p>
<p>四、Rake的顺序<br />
</p>
<pre>task <span class="sy">:purchaseAlcohol</span> <span class="r">do</span><tt>
</tt>  puts <span class="s"><span class="dl">"</span><span class="k">Purchased Vodka</span><span class="dl">"</span></span><tt>
</tt><span class="r">end</span><tt>
</tt><tt>
</tt>task <span class="sy">:mixDrink</span> =&gt; <span class="sy">:purchaseAlcohol</span> <span class="r">do</span><tt>
</tt>  puts <span class="s"><span class="dl">"</span><span class="k">Mixed Fuzzy Navel</span><span class="dl">"</span></span><tt>
</tt><span class="r">end</span><tt>
</tt><tt>
</tt>task <span class="sy">:getSmashed</span> =&gt; <span class="sy">:mixDrink</span> <span class="r">do</span><tt>
</tt>  puts <span class="s"><span class="dl">"</span><span class="k">Dood, everthing's blurry, can I halff noth'r drinnnk?</span><span class="dl">"</span></span><tt>
</tt><span class="r">end</span></pre>
<p>这样，如果想喝酒，就得先去买，如果想喝醉，就得先喝酒。<br />
</p>
<pre><span class="er">$</span> rake purchaseAlcohol<tt>
</tt> <span class="co">Purchased</span> <span class="co">Vodka</span><tt>
</tt><span class="er">$</span> rake mixDrink        <tt>
</tt> <span class="co">Purchased</span> <span class="co">Vodka</span><tt>
</tt> <span class="co">Mixed</span> <span class="co">Fuzzy</span> <span class="co">Navel</span><tt>
</tt><span class="er">$</span> rake getSmashed<tt>
</tt> <span class="co">Purchased</span> <span class="co">Vodka</span><tt>
</tt> <span class="co">Mixed</span> <span class="co">Fuzzy</span> <span class="co">Navel</span><tt>
</tt> <span class="co">Dood</span>, everthing<span class="s"><span class="dl">'</span><span class="k">s blurry, can I halff noth</span><span class="dl">'</span></span>r drinnnk?</pre>
<p>看到了吧，我喝醉和，因为酒已经买了，也被我喝了。(译者：我是喜欢百事的，所以倘若我写，定然拿百事当例子。但是我让我儿子和可口，为什么呢？下面告诉你。)</p>
<p>现在，你的欲望无法满足了，你想让你的朋友加入进来。就像一个团队的开发，如果你想加入一个新人，你得有合适的规划。你得有文档。那么问题来了。</p>
<div><img class="blogimg" src="http://www.railsenvy.com/assets/2007/6/8/jason_drunk_2.jpg?1181338316" border="0" alt="" /></div>
<p>五、如何给我的Rake添加文档</p>
<p>Rake添加文档非常的方便，使用“desc”就可以了：</p>
<pre>desc <span class="s"><span class="dl">"</span><span class="k">This task will purchase your Vodka</span><span class="dl">"</span></span><tt>
</tt>task <span class="sy">:purchaseAlcohol</span> <span class="r">do</span><tt>
</tt>  puts <span class="s"><span class="dl">"</span><span class="k">Purchased Vodka</span><span class="dl">"</span></span><tt>
</tt><span class="r">end</span><tt>
</tt><tt>
</tt>desc <span class="s"><span class="dl">"</span><span class="k">This task will mix a good cocktail</span><span class="dl">"</span></span><tt>
</tt>task <span class="sy">:mixDrink</span> =&gt; <span class="sy">:purchaseAlcohol</span> <span class="r">do</span><tt>
</tt>  puts <span class="s"><span class="dl">"</span><span class="k">Mixed Fuzzy Navel</span><span class="dl">"</span></span><tt>
</tt><span class="r">end</span><tt>
</tt><tt>
</tt>desc <span class="s"><span class="dl">"</span><span class="k">This task will drink one too many</span><span class="dl">"</span></span><tt>
</tt>task <span class="sy">:getSmashed</span> =&gt; <span class="sy">:mixDrink</span> <span class="r">do</span><tt>
</tt>  puts <span class="s"><span class="dl">"</span><span class="k">Dood, everthing's blurry, can I halff noth'r drinnnk?</span><span class="dl">"</span></span><tt>
</tt><span class="r">end</span></pre>
<p>看到了吧，我的每个任务都添加了desc，这样我们可以输入&#8221;rake -T&#8221;或者&#8221;rake &#8211;tasks&#8221;:<br />
</p>
<pre><span class="gv">$rake</span> --tasks<tt>
</tt> rake getSmashed        <span class="c"># This task will drink one too many</span><tt>
</tt> rake mixDrink          <span class="c"># This task will mix a good cocktail</span><tt>
</tt> rake purchaseAlcohol  <span class="c"># This task will purchase your Vodka</span></pre>
<p>简单乎？呵呵</p>
<p>六、Rake的命名空间</p>
<p>当你开始酗酒，并且开始使用大量的rake任务的时候，你需要一个好方法将他们分类，这时用到了命名空间，如果我在上面的例子使用了命名空间，那么：</p>
<pre>namespace <span class="sy">:alcoholic</span> <span class="r">do</span><tt>
</tt>  desc <span class="s"><span class="dl">"</span><span class="k">This task will purchase your Vodka</span><span class="dl">"</span></span><tt>
</tt>  task <span class="sy">:purchaseAlcohol</span> <span class="r">do</span><tt>
</tt>    puts <span class="s"><span class="dl">"</span><span class="k">Purchased Vodka</span><span class="dl">"</span></span><tt>
</tt>  <span class="r">end</span><tt>
</tt><tt>
</tt>  desc <span class="s"><span class="dl">"</span><span class="k">This task will mix a good cocktail</span><span class="dl">"</span></span><tt>
</tt>  task <span class="sy">:mixDrink</span> =&gt; <span class="sy">:purchaseAlcohol</span> <span class="r">do</span><tt>
</tt>    puts <span class="s"><span class="dl">"</span><span class="k">Mixed Fuzzy Navel</span><span class="dl">"</span></span><tt>
</tt>  <span class="r">end</span><tt>
</tt><tt>
</tt>  desc <span class="s"><span class="dl">"</span><span class="k">This task will drink one too many</span><span class="dl">"</span></span><tt>
</tt>  task <span class="sy">:getSmashed</span> =&gt; <span class="sy">:mixDrink</span> <span class="r">do</span><tt>
</tt>    puts <span class="s"><span class="dl">"</span><span class="k">Dood, everthing's blurry, can I halff noth'r drinnnk?</span><span class="dl">"</span></span><tt>
</tt>  <span class="r">end</span><tt>
</tt><span class="r">end</span></pre>
<p>命名空间允许你将一些任务放到特定的分类中，在一个Rakefile中，你可以加入几个命名空间。运行rake &#8211;tasks</p>
<pre>rake alcoholic<span class="sy">:getSmashed</span>        <span class="c"># This task will drink one too many</span><tt>
rake alcoholic<span class="sy">:mixDrink</span>          <span class="c"># This task will mix a good cocktail</span><tt>
</tt>rake alcoholic<span class="sy">:purchaseAlcohol</span>  <span class="c"># This task will purchase your Vodka</span></tt></pre>
<p><tt>所以如果想运行这个任务，只要输入 rake alcoholic:getSmashed：</p>
<p>七、如何写一个有用的ruby任务</p>
<p>最近，我想用ruby创建几个文件夹：</p>
<p></tt></p>
<pre><tt>desc <span class="s"><span class="dl">"</span><span class="k">Create blank directories if they don't already exist</span><span class="dl">"</span></span><tt>
</tt>task(<span class="sy">:create_directories</span>) <span class="r">do</span><tt>
</tt>  <tt>
</tt>  <span class="c"># The folders I need to create</span><tt>
</tt>  shared_folders = [<span class="s"><span class="dl">"</span><span class="k">icons</span><span class="dl">"</span></span>,<span class="s"><span class="dl">"</span><span class="k">images</span><span class="dl">"</span></span>,<span class="s"><span class="dl">"</span><span class="k">groups</span><span class="dl">"</span></span>]<tt>
</tt>  <tt>
</tt>  <span class="r">for</span> folder <span class="r">in</span> shared_folders<tt>
</tt>    <tt>
</tt>    <span class="c"># Check to see if it exists</span><tt>
</tt>    <span class="r">if</span> <span class="co">File</span>.exists?(folder)<tt>
</tt>      puts <span class="s"><span class="dl">"</span><span class="il"><span class="dl">#{</span>folder<span class="dl">}</span></span><span class="k"> exists</span><span class="dl">"</span></span><tt>
</tt>    <span class="r">else</span><tt>
</tt>      puts <span class="s"><span class="dl">"</span><span class="il"><span class="dl">#{</span>folder<span class="dl">}</span></span><span class="k"> doesn't exist so we're creating</span><span class="dl">"</span></span><tt>
</tt>      <span class="co">Dir</span>.mkdir <span class="s"><span class="dl">"</span><span class="il"><span class="dl">#{</span>folder<span class="dl">}</span></span><span class="dl">"</span></span><tt>
</tt>    <span class="r">end</span><tt>
</tt>    <tt>
</tt>  <span class="r">end</span><tt>
</tt><span class="r">end</span></tt></pre>
<p><tt>当然，还可以在rake中使用更多的 文件工具<a href="http://corelib.rubyonrails.org/classes/FileUtils.html" target="_blank">File Utils</a>，或者加入其他的部分。</p>
<p>八、如何为我的rails应用写一个Rake任务</p>
<p>一个rails应用中，已经有了一些rake任务，你可以在你的项目目录里运行：rake --tasks。</p>
<p>为了给你的rails应用添加一个新的任务，你可以打开/lib/tasks目录（已经存在的），添加一个叫</tt>something.rake的文件，这个任务会被自动的检索到，这些任务会被添加到rake tasks列表中，你可以在根目录里运行他们，现在把我们上面的例子放到这个rails应用中。</p>
<p><strong>utils.rake</strong><br />
<tt><br />
</tt></p>
<pre>namespace <span class="sy">:utils</span> <span class="r">do</span><tt>
</tt>  desc <span class="s"><span class="dl">"</span><span class="k">Create blank directories if they don't already exist</span><span class="dl">"</span></span><tt>
</tt>  task(<span class="sy">:create_directories</span>) <span class="r">do</span><tt>
</tt>  <tt>
</tt>    <span class="c"># The folders I need to create</span><tt>
</tt>    shared_folders = [<span class="s"><span class="dl">"</span><span class="k">icons</span><span class="dl">"</span></span>,<span class="s"><span class="dl">"</span><span class="k">images</span><span class="dl">"</span></span>,<span class="s"><span class="dl">"</span><span class="k">groups</span><span class="dl">"</span></span>]<tt>
</tt>          <tt>
</tt>    <span class="r">for</span> folder <span class="r">in</span> shared_folders<tt>
</tt>        <tt>
</tt>      <span class="c"># Check to see if it exists</span><tt>
</tt>      <span class="r">if</span> <span class="co">File</span>.exists?(<span class="s"><span class="dl">"</span><span class="il"><span class="dl">#{</span><span class="co">RAILS_ROOT</span><span class="dl">}</span></span><span class="k">/public/</span><span class="il"><span class="dl">#{</span>folder<span class="dl">}</span></span><span class="dl">"</span></span>)<tt>
</tt>        puts <span class="s"><span class="dl">"</span><span class="il"><span class="dl">#{</span><span class="co">RAILS_ROOT</span><span class="dl">}</span></span><span class="k">/public/</span><span class="il"><span class="dl">#{</span>folder<span class="dl">}</span></span><span class="k"> exists</span><span class="dl">"</span></span><tt>
</tt>      <span class="r">else</span><tt>
</tt>        puts <span class="s"><span class="dl">"</span><span class="il"><span class="dl">#{</span><span class="co">RAILS_ROOT</span><span class="dl">}</span></span><span class="k">/public/</span><span class="il"><span class="dl">#{</span>folder<span class="dl">}</span></span><span class="k"> doesn't exist so we're creating</span><span class="dl">"</span></span><tt>
</tt>        <span class="co">Dir</span>.mkdir <span class="s"><span class="dl">"</span><span class="il"><span class="dl">#{</span><span class="co">RAILS_ROOT</span><span class="dl">}</span></span><span class="k">/public/</span><span class="il"><span class="dl">#{</span>folder<span class="dl">}</span></span><span class="dl">"</span></span><tt>
</tt>      <span class="r">end</span><tt>
</tt>         <tt>
</tt>    <span class="r">end</span><tt>
</tt>  <span class="r">end</span><tt>
</tt><span class="r">end</span></pre>
<p>注意上面的代码，我使用了#{RAILS_ROOT} 来得到rails应用的当前位置，现在运行“rake &#8211;tasks”，你可以看到我们的任务已经添加到tasks列表中了。</p>
<pre>...<tt>
</tt>rake tmp<span class="sy">:pids</span><span class="sy">:clear</span>              <span class="c"># Clears all files in tmp/pids</span><tt>
</tt>rake tmp<span class="sy">:sessions</span><span class="sy">:clear</span>          <span class="c"># Clears all files in tmp/sessions</span><tt>
</tt>rake tmp<span class="sy">:sockets</span><span class="sy">:clear</span>           <span class="c"># Clears all files in tmp/sockets</span><tt>
</tt>rake utils<span class="sy">:create_directories</span>    <span class="c"># Create blank directories if they don't already exist</span><tt>
</tt>...</pre>
<p><tt>九、如何在任务中调用rails的model</p>
<p>呵呵，这个正是我最多使用rake的地方，写一个rake任务，代替原来需要手工操作的地方，或者一些任务代替经常需要按照计划自动执行（使用</tt><strong> <a href="http://www.aota.net/Script_Installation_Tips/cronhelp.php3" target="_new">cronjobs</a></strong><tt>）的事情。就像我开头说的那样我用rake任务执行下面的擦作：</p>
<p></tt>1、给列表中的用户发送邮件<br />
2、每晚数据的计算和报告<br />
3、过期或重新生成缓存<br />
4、备份数据和svn版本(<span style="text-decoration: underline">how&#8217;s this : subversion repository</span>)<br />
5、运行数据处理脚本(sort of,how much is called this )<br />
<tt><br />
这个补充了原来的功能，而且相当简单。下面这个任务是检查用户的过期时间，对快过期的用户发送邮件。</p>
<p>utils.rake</p>
<p></tt></p>
<pre>namespace <span class="sy">:utils</span> <span class="r">do</span><tt>
</tt>  desc <span class="s"><span class="dl">"</span><span class="k">Finds soon to expire subscriptions and emails users</span><span class="dl">"</span></span><tt>
</tt>  task(<span class="sy">:send_expire_soon_emails</span> =&gt; <span class="sy">:environment</span>) <span class="r">do</span><tt>
</tt>        <span class="c"># Find users to email</span><tt>
</tt>        <span class="r">for</span> user <span class="r">in</span> <span class="co">User</span>.members_soon_to_expire<tt>
</tt>                puts <span class="s"><span class="dl">"</span><span class="k">Emailing </span><span class="il"><span class="dl">#{</span>user.name<span class="dl">}</span></span><span class="dl">"</span></span><tt>
</tt>                <span class="co">UserNotifier</span>.deliver_expire_soon_notification(user)<tt>
</tt>        <span class="r">end</span><tt>
</tt>  <span class="r">end</span><tt>
</tt><span class="r">end</span></pre>
<p>使用你的model只用一步，&#8221;=&gt; :environment&#8221;<br />
<tt><br />
</tt>task(:send_expire_soon_emails =&gt; :environment) do<br />
<tt><br />
如果在我的开发环境上运行这个任务，我只需要</tt><strong>&#8220;rake utils:send_expire_soon_emails&#8221;</strong>，如果我想在产品环境下运行这个任务，我需要<strong>&#8220;rake RAILS_ENV=production utils:send_expire_soon_emails&#8221;</strong><tt>。</p>
<p>如果你想在每晚都运行这个任务，你需要写一个</tt> <a href="http://www.aota.net/Script_Installation_Tips/cronhelp.php3" target="_new">cronjob</a> ，像这样：<br />
<tt><br />
</tt></p>
<pre><span class="i">0</span> <span class="i">0</span> * * * cd <span class="rx"><span class="dl">/</span><span class="k">var</span><span class="dl">/</span></span>www/apps/rails_app/ &amp;&amp; <span class="rx"><span class="dl">/</span><span class="k">usr</span><span class="dl">/</span></span>local/bin/rake <span class="co">RAILS_ENV</span>=production utils<span class="sy">:send_expire_soon_emails</span></pre>
<p>相当的方便。<br />
<tt><br />
十、在哪找到一些例子</p>
<p>现在对一个有用的rake任务已经了解很多了，那么我将给你几个资源，我想最好的学习方法是看看别人的代码。</p>
<p></tt><a href="http://dev.rubyonrails.org/changeset/6849" target="_new">brand new rake tasks</a> 在edge rails 中，这个可以创建和重置你的数据库。</p>
<p>Craig Ambrose写的数据库备份， <a href="http://blog.craigambrose.com/articles/2007/03/01/a-rake-task-for-database-backups">database backups</a>。<br />
<tt><br />
</tt>Adam Greene写了一组任务 <a href="http://www.rubyinside.com/advent2006/15-s3rake.html" target="_new">set of Rake tasks</a>，可以将所有的数据备份到amazon S3。他还给了我一个升级版本，你可以在这下载<a href="http://www.railsenvy.com/assets/2007/6/19/s3_rake_tasks.zip">here</a>。<br />
<tt><br />
</tt>Jay Fields的任务测试，<a href="http://blog.jayfields.com/2006/11/ruby-testing-rake-tasks.html" target="_new">testing rake tasks</a>。<br />
<tt><br />
</tt><a href="http://errtheblog.com/post/33" target="_new">a new way of setting the RAILS_ENV and teaches how to use rake to boot you into a Mysql shell</a> （看的时候留意一下他的注释）</p>
<p><a href="http://docs.rubyrake.org/" target="_new">Rake Bookshelf Books</a>，和Martin Fowler的<a href="http://www.martinfowler.com/articles/rake.html">Using the Rake Build Language</a> 教程，这两个都很有用，虽然有点过时。</p>
<p>如果你发现其他更好的文章，发贴子给我们。</p>
<p>译者：恩，这段不用翻译，懂的朋友自然会去看的了。</p>
<p>Still reading? If you are, I wanted to let you know that we&#8217;re looking for more people to write for RailsEnvy. If you have an idea for a good rails tutorial we want to hear from you! Basically we would work with you to flesh out the tutorial and help polish (acting as an editor). It could definitely be a great way to get your name out there, and start getting some hits (for your blog or company). Email Gregg at RailsEnvy if you&#8217;re interested.<br />
<tt><br />
另：我刚收到jim的邮件，如何更简单的创建我的目录。<br />
</tt></p>
<pre><span class="c"># This is needed because the existing version of directory in Rake is slightly broken, but Jim says it'll be fixed in the next version.</span><tt>
</tt><span class="r">alias</span> <span class="sy"> <img src='http://blog.thinklet.net/franky/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> riginal_directory</span> <span class="sy">:directory</span><tt>
</tt><span class="r">def</span> <span class="fu">directory</span>(dir)<tt>
</tt>  original_directory dir<tt>
</tt>  <span class="co">Rake</span>::<span class="co">Task</span>[dir]<tt>
</tt><span class="r">end</span><tt>
</tt><tt>
</tt><span class="c"># Do the directory creation</span><tt>
</tt>namespace <span class="sy">:utils</span> <span class="r">do</span><tt>
</tt>  task <span class="sy">:create_directories</span> =&gt; [<tt>
</tt>    directory(<span class="s"><span class="dl">'</span><span class="k">public/icons</span><span class="dl">'</span></span>),<tt>
</tt>    directory(<span class="s"><span class="dl">'</span><span class="k">public/images</span><span class="dl">'</span></span>),<tt>
</tt>    directory(<span class="s"><span class="dl">'</span><span class="k">public/groups</span><span class="dl">'</span></span>),<tt>
</tt>  ]<tt>
</tt><span class="r">end</span></pre>
<p><tt><br />
注：图片均来自英文原文(Pics come from the english page)</tt></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thinklet.net/franky/2009/03/01/ruby-on-rails%e4%b8%adrake%e7%9a%84%e7%bb%8f%e5%85%b8%e6%95%99%e7%a8%8b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>如何解决Ruby on Rails &#8211; Mysql::Error: Lost connection to MySQL server during query:</title>
		<link>http://blog.thinklet.net/franky/2009/02/10/ruby-on-rails-mysqlerror-lost-connection-to-mysql-server-during-query/</link>
		<comments>http://blog.thinklet.net/franky/2009/02/10/ruby-on-rails-mysqlerror-lost-connection-to-mysql-server-during-query/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 16:47:35 +0000</pubDate>
		<dc:creator>frankychen</dc:creator>
				<category><![CDATA[Ruby&Rails]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://blog.thinklet.net/franky/?p=10</guid>
		<description><![CDATA[Ruby on Rails &#8211; Mysql::Error: Lost connection to MySQL server during query:
前段时间，我们的CE平台不断出现上述错误。原因是：mysql server 的链接断开后，Rails的Active Record没有检测到，断续执行数据库查询。
Mysql链接保持时间默认为3600秒，在/etc/my.cnf 里可以修改名为wait_time参数的值以改变链接保持的时间。
Rails中Active Record的并不会自动检测与数据库的链接是否是活动的，断开后也不会自动重新链接；如果mysql链接超时，自动断开后，Active Record并不知道，再执行时，就会抛出Mysql::Error: Lost connection to MySQL server during query:的错误。
解决方法时：修改Active Record检证时间。
     修改config/environment.rb配置文件，将config.active_record.verification_timeout = XXX 这句加入到Rails::Initializer.run do &#124;config&#124;  &#8230;.  end中， 值设为小于或等于mysql wait time的值 。
    
例如，mysql server 的wait_time=120，那我们rails的设置应该如下：
Rails::Initializer.run do &#124;config&#124; 
     config.active_record.verification_timeout = 118   #注，理论上小于等于120都没问题的
    &#8230;&#8230;&#8230;
end
]]></description>
			<content:encoded><![CDATA[<p>Ruby on Rails &#8211; Mysql::Error: Lost connection to MySQL server during query:</p>
<p>前段时间，我们的CE平台不断出现上述错误。原因是：mysql server 的链接断开后，Rails的Active Record没有检测到，断续执行数据库查询。</p>
<p>Mysql链接保持时间默认为3600秒，在/etc/my.cnf 里可以修改名为wait_time参数的值以改变链接保持的时间。<br />
Rails中Active Record的并不会自动检测与数据库的链接是否是活动的，断开后也不会自动重新链接；如果mysql链接超时，自动断开后，Active Record并不知道，再执行时，就会抛出Mysql::Error: Lost connection to MySQL server during query:的错误。</p>
<p>解决方法时：修改Active Record检证时间。<br />
     修改config/environment.rb配置文件，将config.active_record.verification_timeout = XXX 这句加入到Rails::Initializer.run do |config|  &#8230;.  end中， 值设为小于或等于mysql wait time的值 。<br />
    </p>
<p>例如，mysql server 的wait_time=120，那我们rails的设置应该如下：<br />
Rails::Initializer.run do |config| </p>
<p>     config.active_record.verification_timeout = 118   #注，理论上小于等于120都没问题的</p>
<p>    &#8230;&#8230;&#8230;</p>
<p>end</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thinklet.net/franky/2009/02/10/ruby-on-rails-mysqlerror-lost-connection-to-mysql-server-during-query/feed/</wfw:commentRss>
		<slash:comments>415</slash:comments>
		</item>
	</channel>
</rss>
<a style="display: none;" href="http://mcinside.com/">MC Inside</a>