<?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/tag/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>瞎忙</title>
		<link>http://cashplk.com/2009/09/04/%e7%9e%8e%e5%bf%99/</link>
		<comments>http://cashplk.com/2009/09/04/%e7%9e%8e%e5%bf%99/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 01:22:37 +0000</pubDate>
		<dc:creator>cashplk</dc:creator>
				<category><![CDATA[work]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[重构]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://cashplk.com/2009/09/04/%e7%9e%8e%e5%bf%99/</guid>
		<description><![CDATA[&#160;&#160;&#160;&#160;&#160; 最近新项目开始，难得老大重视。我也当上半个主程了。系统设计啊，业务熟悉啊，这次的项目就我最熟悉业务。所以也理所应当承担起大部分的业务逻辑。写了很多代码，挺高兴的。虽然很累。 &#160;&#160;&#160;&#160;&#160; 说到代码，第一次写这么多，还是看到了很多问题，关于代码的质量，如何抽取逻辑，提取方法。虽然以前看了很多东西，毕竟这次是理论结合实践了吧，项目已经提交测试了。每天还乐此不疲的进行代码的重构和修改。希望忙过这阵子，可以好好看看ruby和erlang。]]></description>
			<content:encoded><![CDATA[<blockquote><p>&#160;&#160;&#160;&#160;&#160; 最近新项目开始，难得老大重视。我也当上半个主程了。系统设计啊，业务熟悉啊，这次的项目就我最熟悉业务。所以也理所应当承担起大部分的业务逻辑。写了很多代码，挺高兴的。虽然很累。</p>
<p>&#160;&#160;&#160;&#160;&#160; 说到代码，第一次写这么多，还是看到了很多问题，关于代码的质量，如何抽取逻辑，提取方法。虽然以前看了很多东西，毕竟这次是理论结合实践了吧，项目已经提交测试了。每天还乐此不疲的进行代码的重构和修改。希望忙过这阵子，可以好好看看ruby和erlang。</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://cashplk.com/2009/09/04/%e7%9e%8e%e5%bf%99/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>
	</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! -->