<?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>cashplk的心路历程 &#187; Ruby</title>
	<atom:link href="http://cashplk.com/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://cashplk.com</link>
	<description>学无止境，梦如夏花～</description>
	<lastBuildDate>Sat, 31 Jul 2010 10:15:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>一个好的Rails3入门</title>
		<link>http://cashplk.com/2010/03/29/%e4%b8%80%e4%b8%aa%e5%a5%bd%e7%9a%84rails3%e5%85%a5%e9%97%a8/</link>
		<comments>http://cashplk.com/2010/03/29/%e4%b8%80%e4%b8%aa%e5%a5%bd%e7%9a%84rails3%e5%85%a5%e9%97%a8/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 01:29:57 +0000</pubDate>
		<dc:creator>cashplk</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[getting started]]></category>
		<category><![CDATA[rails3]]></category>

		<guid isPermaLink="false">http://cashplk.com/?p=137</guid>
		<description><![CDATA[Getting Started with Rails 3: http://edgeguides.rubyonrails.org/getting_started.html]]></description>
			<content:encoded><![CDATA[<p>Getting Started with  Rails 3:<br />
<br/></p>
<p><a href="http://edgeguides.rubyonrails.org/getting_started.html">http://edgeguides.rubyonrails.org/getting_started.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://cashplk.com/2010/03/29/%e4%b8%80%e4%b8%aa%e5%a5%bd%e7%9a%84rails3%e5%85%a5%e9%97%a8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用Ruby分析文件＆去重</title>
		<link>http://cashplk.com/2009/06/24/%e4%bd%bf%e7%94%a8ruby%e5%88%86%e6%9e%90%e6%96%87%e4%bb%b6%ef%bc%86%e5%8e%bb%e9%87%8d/</link>
		<comments>http://cashplk.com/2009/06/24/%e4%bd%bf%e7%94%a8ruby%e5%88%86%e6%9e%90%e6%96%87%e4%bb%b6%ef%bc%86%e5%8e%bb%e9%87%8d/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 03:14:16 +0000</pubDate>
		<dc:creator>cashplk</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[去重]]></category>
		<category><![CDATA[分析]]></category>
		<category><![CDATA[uniq]]></category>

		<guid isPermaLink="false">http://cashplk.com/?p=62</guid>
		<description><![CDATA[需要分析一个18M的文件，其中内容类似如下： 2009-06-20 00:00:07,678 [5409241381(T,105101,**,N,**,18ms)(12345678901234567890,dummy,**,888888,-,1000.00,**,-,005004-**,5409241381,0,null)] 文件以=== All done!!! ===结束。 全部日志加起来有7W多条，由于是时间程序批量请求的，所以其中肯定有很多重复的数据。我需要的只是那个12345678901234567890的20位数字而已。 so,尝试一下Ruby的脚本能力。 file = File.open&#40;result, 'r'&#41; ＃ 读取文件 &#160; ＃ 使用正则表达式匹配20个数字 regex = /\d&#123;20&#125;/ &#160; $array = Array.new&#40;&#41;; &#160; while&#40;line = file.gets&#41; ＃ 读取文件 $array.push regex.match&#40;line&#41;.to_s break if line == '=== All done!!! ===' end file.close &#160; # 去重 $resultFile.puts&#40;$array.uniq&#41; puts 'convert ends!!']]></description>
			<content:encoded><![CDATA[<p>需要分析一个18M的文件，其中内容类似如下：</p>
<p>2009-06-20 00:00:07,678 [5409241381(T,105101,**,N,**,18ms)(12345678901234567890,dummy,**,888888,-,1000.00,**,-,005004-**,5409241381,0,null)]</p>
<p>文件以=== All done!!! ===结束。</p>
<p>全部日志加起来有7W多条，由于是时间程序批量请求的，所以其中肯定有很多重复的数据。我需要的只是那个12345678901234567890的20位数字而已。<br />
so,尝试一下Ruby的脚本能力。</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  file = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>result, <span style="color:#996600;">'r'</span><span style="color:#006600; font-weight:bold;">&#41;</span> ＃ 读取文件
&nbsp;
＃ 使用正则表达式匹配<span style="color:#006666;">20</span>个数字
regex = <span style="color:#006600; font-weight:bold;">/</span>\d<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">20</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">/</span>
&nbsp;
<span style="color:#ff6633; font-weight:bold;">$array</span> = <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span>line = file.<span style="color:#CC0066; font-weight:bold;">gets</span><span style="color:#006600; font-weight:bold;">&#41;</span>  ＃ 读取文件
    <span style="color:#ff6633; font-weight:bold;">$array</span>.<span style="color:#9900CC;">push</span> regex.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>line<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_s</span>
    <span style="color:#9966CC; font-weight:bold;">break</span> <span style="color:#9966CC; font-weight:bold;">if</span> line == <span style="color:#996600;">'=== All done!!! ==='</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  file.<span style="color:#9900CC;">close</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># 去重</span>
<span style="color:#ff6633; font-weight:bold;">$resultFile</span>.<span style="color:#CC0066; font-weight:bold;">puts</span><span style="color:#006600; font-weight:bold;">&#40;</span>$array.<span style="color:#9900CC;">uniq</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'convert ends!!'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cashplk.com/2009/06/24/%e4%bd%bf%e7%94%a8ruby%e5%88%86%e6%9e%90%e6%96%87%e4%bb%b6%ef%bc%86%e5%8e%bb%e9%87%8d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>About: Ruby China Conf</title>
		<link>http://cashplk.com/2009/05/25/about-ruby-china-conf/</link>
		<comments>http://cashplk.com/2009/05/25/about-ruby-china-conf/#comments</comments>
		<pubDate>Mon, 25 May 2009 06:08:13 +0000</pubDate>
		<dc:creator>cashplk</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[china]]></category>
		<category><![CDATA[shanghai]]></category>

		<guid isPermaLink="false">http://cashplk.com/?p=51</guid>
		<description><![CDATA[ruby conf china 2009的网站： http://rubyconfchina.org/ 演讲者以及对应的文档： http://rubyconfchina.org/speakers]]></description>
			<content:encoded><![CDATA[<p>ruby conf china 2009的网站：</p>
<p><a href="http://rubyconfchina.org/">http://rubyconfchina.org/</a></p>
<p>演讲者以及对应的文档：</p>
<p><a href="http://rubyconfchina.org/speakers">http://rubyconfchina.org/speakers</a></p>
]]></content:encoded>
			<wfw:commentRss>http://cashplk.com/2009/05/25/about-ruby-china-conf/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ruby操作CouchDB</title>
		<link>http://cashplk.com/2009/05/23/ruby%e6%93%8d%e4%bd%9ccouchdb/</link>
		<comments>http://cashplk.com/2009/05/23/ruby%e6%93%8d%e4%bd%9ccouchdb/#comments</comments>
		<pubDate>Sat, 23 May 2009 06:18:44 +0000</pubDate>
		<dc:creator>cashplk</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[couchdb]]></category>
		<category><![CDATA[irb]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://cashplk.com/?p=44</guid>
		<description><![CDATA[以下是一个简单的使用Ruby操作CouchDB数据库的代码： 新建fromRuby2CouchDB.rb文件，具体内容如下： require 'net/http' &#160; module Couch class Server def initialize&#40;host, port, option = nil&#41; @host = host; @port = port; @option = option; end &#160; def delete&#40;uri&#41; request&#40;Net::HTTP::Delete.new&#40;uri&#41;&#41; end &#160; def get&#40;uri&#41; request&#40;Net::HTTP::Get.new&#40;uri&#41;&#41; end &#160; # put json to request def put&#40;uri, json&#41; req = Net::HTTP::Put.new&#40;uri&#41; req&#91;&#34;content.type&#34;&#93; = &#34;application/json&#34; req.body = json request&#40;req&#41; end [...]]]></description>
			<content:encoded><![CDATA[<p>以下是一个简单的使用Ruby操作CouchDB数据库的代码：</p>
<p>新建fromRuby2CouchDB.rb文件，具体内容如下：</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'net/http'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> Couch
  <span style="color:#9966CC; font-weight:bold;">class</span> Server
    <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>host, port, option = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@host</span> = host;
      <span style="color:#0066ff; font-weight:bold;">@port</span> = port;
      <span style="color:#0066ff; font-weight:bold;">@option</span> = option;
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> delete<span style="color:#006600; font-weight:bold;">&#40;</span>uri<span style="color:#006600; font-weight:bold;">&#41;</span>
      request<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Net::HTTP::Delete</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>uri<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> get<span style="color:#006600; font-weight:bold;">&#40;</span>uri<span style="color:#006600; font-weight:bold;">&#41;</span>
      request<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Net::HTTP::Get</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>uri<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># put json to request</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> put<span style="color:#006600; font-weight:bold;">&#40;</span>uri, json<span style="color:#006600; font-weight:bold;">&#41;</span>
      req = <span style="color:#6666ff; font-weight:bold;">Net::HTTP::Put</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>uri<span style="color:#006600; font-weight:bold;">&#41;</span>
      req<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;content.type&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;application/json&quot;</span>
      req.<span style="color:#9900CC;">body</span> = json
      request<span style="color:#006600; font-weight:bold;">&#40;</span>req<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> post<span style="color:#006600; font-weight:bold;">&#40;</span>uri, json<span style="color:#006600; font-weight:bold;">&#41;</span>
      req = <span style="color:#6666ff; font-weight:bold;">Net::HTTP::Post</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>uri<span style="color:#006600; font-weight:bold;">&#41;</span>
      req<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;content-type&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;application/json&quot;</span>
      req.<span style="color:#9900CC;">body</span> = json
      request<span style="color:#006600; font-weight:bold;">&#40;</span>req<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> request<span style="color:#006600; font-weight:bold;">&#40;</span>req<span style="color:#006600; font-weight:bold;">&#41;</span>
      res = <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">start</span><span style="color:#006600; font-weight:bold;">&#40;</span>@host, <span style="color:#0066ff; font-weight:bold;">@port</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
        <span style="color:#006600; font-weight:bold;">|</span>http<span style="color:#006600; font-weight:bold;">|</span>http.<span style="color:#9900CC;">request</span><span style="color:#006600; font-weight:bold;">&#40;</span>req<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#9966CC; font-weight:bold;">not</span> res.<span style="color:#9900CC;">kind_of</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Net::HTTPSuccess</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        handle_error<span style="color:#006600; font-weight:bold;">&#40;</span>req, res<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      res
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    private
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> handle_error<span style="color:#006600; font-weight:bold;">&#40;</span>req, res<span style="color:#006600; font-weight:bold;">&#41;</span>
      e = <span style="color:#CC00FF; font-weight:bold;">RuntimeError</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{res.code}:#{res.message}<span style="color:#000099;">\n</span>METHOD:&quot;</span> <span style="color:#006600; font-weight:bold;">+</span>
&nbsp;
          <span style="color:#996600;">&quot;#{req.method}<span style="color:#000099;">\n</span>URI:#{req.path}<span style="color:#000099;">\n</span>#{res.body}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#CC0066; font-weight:bold;">raise</span> e
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>在文件路径下，以命令行模式运行：irb，启动ruby编译器：<br />
irb(main):001:0&gt; require &#8220;fromRuby2CouchDB.rb&#8221;<br />
=&gt; true<br />
irb(main):002:0&gt; server = Couch::Server.new(&#8220;127.0.0.1&#8243;,&#8221;5984&#8243;)<br />
=&gt; #&lt;Couch::Server:0xb7bdd258 @option=nil, @port=&#8221;5984&#8243;, @host=&#8221;127.0.0.1&#8243;&gt;<br />
irb(main):003:0&gt; server.get(&#8220;/blog/&#8221;)<br />
会插入一个新的数据库，名为blog。如果不对，会打印出相应的错误。成功后，可以在页面查看是否存在该数据库。</p>
]]></content:encoded>
			<wfw:commentRss>http://cashplk.com/2009/05/23/ruby%e6%93%8d%e4%bd%9ccouchdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ruby惯用写法</title>
		<link>http://cashplk.com/2009/05/23/ruby%e6%83%af%e7%94%a8%e5%86%99%e6%b3%95/</link>
		<comments>http://cashplk.com/2009/05/23/ruby%e6%83%af%e7%94%a8%e5%86%99%e6%b3%95/#comments</comments>
		<pubDate>Sat, 23 May 2009 05:09:12 +0000</pubDate>
		<dc:creator>cashplk</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby 习惯]]></category>

		<guid isPermaLink="false">http://cashplk.com/?p=26</guid>
		<description><![CDATA[放狗搜inject的时候找到的。 原帖：http://www.javaeye.com/topic/24642?page=1 Ruby有不少惯用法，这里略作一些介绍，也方便阅读他人代码： 迭代 一般写法: for i in (1..10) puts i end 习惯写法: (1..10).each{&#124;i&#124; puts i} 或 1.upto(10){&#124;i&#124; puts i} &#124;&#124;=赋值 一般写法： number = 1 if number.nil? number = 1 unless number 习惯写法： number &#124;&#124;= 1]]></description>
			<content:encoded><![CDATA[<p>放狗搜inject的时候找到的。<br />
原帖：http://www.javaeye.com/topic/24642?page=1</p>
<p>Ruby有不少惯用法，这里略作一些介绍，也方便阅读他人代码：</p>
<p><strong><span>迭代</span></strong><br />
一般写法:<br />
for i in (1..10) puts i end</p>
<p>习惯写法:</p>
<p>(1..10).each{|i| puts i} 或 1.upto(10){|i| puts i}</p>
<p><strong><span>||=赋值</span></strong><br />
一般写法：<br />
number = 1 if number.nil? number = 1 unless number<br />
习惯写法：</p>
<p>number ||= 1</p>
]]></content:encoded>
			<wfw:commentRss>http://cashplk.com/2009/05/23/ruby%e6%83%af%e7%94%a8%e5%86%99%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>对于inject的使用</title>
		<link>http://cashplk.com/2009/05/23/%e5%af%b9%e4%ba%8einject%e7%9a%84%e4%bd%bf%e7%94%a8/</link>
		<comments>http://cashplk.com/2009/05/23/%e5%af%b9%e4%ba%8einject%e7%9a%84%e4%bd%bf%e7%94%a8/#comments</comments>
		<pubDate>Sat, 23 May 2009 04:28:17 +0000</pubDate>
		<dc:creator>cashplk</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Alex]]></category>
		<category><![CDATA[inject]]></category>

		<guid isPermaLink="false">http://cashplk.com/?p=6</guid>
		<description><![CDATA[看到Alex的Alex学Ruby[ inject需要注意的地方 ] ，节选部分如下： inject([]) do &#124;item ,i &#124;这样的写法，每一步，item都会被设置为block的返回值。 如下： 1 2 3 4 5 6 7 8 9 arr1 = &#91;&#93; arr2 = &#91;1,2,3&#93; &#160; arr2.each do &#124;i&#124; arr1 &#60;&#60; i+1 end &#160; p arr1 #=&#62; [2, 3, 4] 使用inject，则可以写为： 1 2 3 4 5 6 7 8 arr2 = &#91;1,2,3&#93; &#160; arr = [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size: x-small;"> </span><span style="font-size: x-small;">看到<a title="Alex" href="http://blackanger.blog.51cto.com/" target="_blank">Alex</a>的<a title="Alex学Ruby[ inject需要注意的地方 ]" href="http://blackanger.blog.51cto.com/140924/90539" target="_blank">Alex学Ruby[ inject需要注意的地方 ]</a> ，节选部分如下：<br />
</span><span style="font-size: x-small;"><br />
</span><span style="font-size: x-small;"> </span><span style="font-size: x-small;">inject([]) do |item ,i |这样的写法，每一步，item都会被设置为block的返回值。</span></p>
<p>如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">    arr1 = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    arr2 = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>  
&nbsp;
    arr2.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
        arr1 <span style="color:#006600; font-weight:bold;">&lt;&lt;</span>  i<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006666;">1</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#CC0066; font-weight:bold;">p</span> arr1
    <span style="color:#008000; font-style:italic;">#=&gt; [2, 3, 4]</span></pre></td></tr></table></div>

<p>使用inject，则可以写为：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">    arr2 = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    arr = arr2.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><span style="color:#006600; font-weight:bold;">|</span>arr1, i<span style="color:#006600; font-weight:bold;">|</span>
        arr1 <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> i <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span> 
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
     <span style="color:#CC0066; font-weight:bold;">p</span> arr1
     <span style="color:#008000; font-style:italic;">#=&gt;[2,3,4]</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://cashplk.com/2009/05/23/%e5%af%b9%e4%ba%8einject%e7%9a%84%e4%bd%bf%e7%94%a8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->